본문 바로가기
IT관련/Java

[확고의 쓱~ 훑어보는 기록] JAVA - 반복문 (if~else 사용 예제)

by 확고 2022. 8. 5.
728x90
반응형
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
import java.util.Scanner;
 
public class Ex_1_ifelse {
    public static void main(String[] args) {
        
        int a,b ;
        Scanner sc = new Scanner (System.in);
        System.out.print("a=");
        a = sc.nextInt();
        
        System.out.print("b=");
        b = sc.nextInt();
        
        if(a>b) { //참이면 중괄호 안이 실행되고 그렇지 않은 경우에는 else 중괄호 안이 실행됨.
                  //실행하고 싶은 게 한 줄이라면 중괄호를 쓰지 않아도 됨.
            System.out.println("a가 더 크다.");
        }
        else { //그렇지 않으면(a가 b보다 크다가 아니면 a<=b)
            System.out.println("b가 더 크다. = 같을 때도 출력됨(세분화 필요)");
        }
        
        System.out.println("--------------------------------------");
        
                
        if(a>b) { //참이면 중괄호 안이 실행되고 그렇지 않은 경우에는 else 중괄호 안이 실행됨.
            //실행하고 싶은 게 한 줄이라면 중괄호를 쓰지 않아도 됨.
            System.out.print("a가 더 크다.");
        }
        else if(a<b) { //그렇지 않으면 (a가 b보다 크다가 아니면 a<=b)
            System.out.print("b가 더 크다.");
        }
        else { //그렇지 않으면 (a==b)
            System.out.print("a와 b가 같다.");
        }
        
        /*
        
        ▼ 콘솔 창에 아래와 같이 입력한 경우 실행 결과
        
        a=10
        b=20
        b가 더 크다. = 같을 때도 출력됨(세분화 필요)
        --------------------------------------
        b가 더 크다.
        
        */
    }
}
 
cs

 

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
import java.util.Scanner;
 
public class Ex_2_ifelse {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int num;
        
        System.out.print("숫자입력:");
        num = sc.nextInt();
        
        if11>num & num>0 ) { // 11 > num > 0 이렇게 사용하지 않는다
            if(num != 3) { // 11 > num > 0이면서 3이 아닌 경우에 실행해라
                System.out.println("1~10사이면서 3이 아닌 숫자");
            }
        }
        
        // 11~20사이의 숫자이면 실행해라
        if(num>10 && 21>num) {
        
        System.out.println(num);
        }
            
        // 21~30사이의 숫자이면 실행해라
        if(num>20 & 31>num) {
            System.out.println(num);
        }
        
        // 5의 배수의 숫자이면 실행해라
        if(num%5==0) {
            System.out.println("5의 배수");
        }
        
        // 10의 배수의 숫자이면 실행해라
        if(num%10==0) {
            System.out.println("10의 배수");
        }
        
        /*
        
        ▼ 콘솔 창에 아래와 같이 입력한 경우 실행 결과
        
        숫자입력:5
        1~10사이면서 3이 아닌 숫자
        5의 배수
        
        */
    }
}
cs

 

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
import java.util.Scanner;
 
public class Ex_3_ifelse {
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
 
        System.out.print("이름 입력:");
        sc.next();
        System.out.print("음악점수 입력:");
        int a = sc.nextInt();
        System.out.print("과학점수 입력:");
        int b = sc.nextInt();
        System.out.print("역사점수 입력:");
        int c = sc.nextInt();
 
        double d = (double)(a + b + c)/3;
        System.out.println("합계 :"+(a+b+c));
        System.out.printf("평균 :%.2f",d);
 
        System.out.println();
        System.out.println("---------------------");
 
        //(문제) 점수를 입력받아 학점을 콘솔 창에 출력하시오
        //(>=90 A학점, 80~89 B학점, 70~79 C학점, 60~69 D학점, 60점 미만 F학점으로)
 
        //(방법 1)
        System.out.print("학점을 입력하시오:");
        int i = sc.nextInt();
        
        if(101>&& i>=90) {
            System.out.println("A학점");
        }
        else if(89>= i && i>79) {
            System.out.println("B학점");
        }
        else if(79>= i && i>69) {
            System.out.println("C학점");
        }
        else if(69>= i && i>59) {
            System.out.println("D학점");
        }
        else {
            System.out.println("F학점");
        }
        
        System.out.println("--------------------");
        
        //(방법 2)
        System.out.print("학점을 입력하시오:");
        int k = sc.nextInt();
        
        if(101>&& k>=90) {
            System.out.println("A학점");
        }
        else if(k>79) {
            System.out.println("B학점");
        }
        else if(k>69) {
            System.out.println("C학점");
        }
        else if(k>59) {
            System.out.println("D학점");
        }
        else {
            System.out.println("F학점");
        }
        
        /*
        
        ▼ 콘솔 창에 아래와 같이 입력한 경우 실행 결과
        이름 입력:확고
        음악점수 입력:100
        과학점수 입력:90
        역사점수 입력:98
        합계 :288
        평균 :96.00
        ---------------------
        학점을 입력하시오:91
        A학점
        --------------------
        학점을 입력하시오:59
        F학점
        
        */
    }
}
 
cs

 

▼ (이전 게시물) [확고의 쓱~ 훑어보는 기록] JAVA - 연산자(산술/대입/증감/관계/논리/삼항)

 

[확고의 쓱~ 훑어보는 기록] JAVA - 연산자(산술/대입/증감/관계/논리/삼항)

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..

j-growthdiary.tistory.com

 

▼ (첫 번째 게시물) [확고의 쓱~ 훑어보는 기록] JAVA - 자료형과 기본 입출력(1)

 

[확고의 쓱~ 훑어보는 기록] JAVA - 자료형과 기본 입출력(1)

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 public class exam {     public static void main(Strin..

j-growthdiary.tistory.com

728x90
반응형

댓글