반응형

           

  JAVA Study





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Scanner;
 
public class tistory {
 
    public static void main(String[] args) {
        // while문
        int i=5;
        
        while(i--!=0){ // i를 1씩 감소시켜 0이 아니면 다음 문장 실행
            System.out.println(i+" - I can do it");
        } // i--가 후위형 이므로 조건식이 평가된 후에 i값이 감소 -> 4~0까지 출력
        
        // 카운트 다운
        int j = 11;
        
        System.out.println("카운트다운 시작");
        
        while(j--!=0){
            System.out.println(j);
            
            for(int k=0; k<2_000_000_000;k++){
                ; // 아무 내용 없는 빈 문장
            }
        }
        System.out.println("GAME OVER");
        
        // 사용자가 입력한 숫자 , 각 자리의 합을 구하는 예
        int num = 0, sum = 0;
        System.out.print("숫자를 입력하세요>>");
        
        Scanner scn = new Scanner(System.in);
        String tmp = scn.nextLine();
        num = Integer.parseInt(tmp);
        
        while(num!=0){
            sum +=( num%10); //num을 10으로 나눈 몫을 계속 더해 sum에 저장
            num = num/10// num값 재설정
        }
        System.out.println("각 자리수의 합 : "+sum);
        
        //do-while문
        int input1 = 0, answer1 = 0;
        
        answer1 = (int)(Math.random() * 100+1;
        Scanner scn1 = new Scanner(System.in);
        
        do{
            System.out.println("1과 100사이 정수를 입력하세요>");
            input1 = scn1.nextInt();
            
            if(input1 > answer1){
                System.out.println("더 작은 수로 다시 시도해보세요");
            } else if(input1 < answer1){
                System.out.println("더 큰 수로 다시 시도해보세요");
            }
        } while (input1 != answer1);
            System.out.println("정답입니다.");
        
            
        //break 문
        int s = 0;
        int a = 0;
        
        while (true){
            if(s<100)
                break;
            ++a;
            sum += a;
        }
        
        System.out.println("a="+a);
        System.out.println("sum="+sum);
        
        //continue문
        int menu = 0;
        int number = 0;
        
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("(1) square");
            System.out.println("(2) square root");
            System.out.println("(3) log");
            System.out.print("원하는 메뉴(1~3)를 선택하세요.(종료 : 0)");
            
            String temp = scanner.nextLine();
            menu = Integer.parseInt(temp);
            
            if (!(1<=menu && menu <=3)){
                System.out.println("메뉴를 잘못 선택했어요.(종료 : 0)");
                continue
            } else if(menu == 0){
                System.out.println("프로그램을 종료합니다.");
                break;
            }
            
            System.out.println("선택하신 메뉴는 " + menu +"번 입니다.");
        }
    
    }
}
 
cs
반응형

'프로그래밍 > JAVA' 카테고리의 다른 글

[JAVA] 배열 예제  (0) 2017.07.04
[JAVA] 5 배열 (Array)  (0) 2017.07.03
[JAVA] 제어구조 예제 5 - for문  (0) 2017.07.01
[JAVA] 제어구조 예제 4 - switch문  (1) 2017.06.30
[JAVA] 제어구조 예제 3 - if문  (0) 2017.06.29

+ Recent posts