본문으로 바로가기
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
import java.util.Scanner;
 
public class Calc_if {
 
    public static void main(String[] args) {
        System.out.println("if");
        
        Scanner sc = new Scanner(System.in);    // 키보드 입력을 위한 Scanner 래퍼런스 변수 및 객체 생성
        
        System.out.print("첫 번째 숫자를 입력해주세요 : ");
        int a = sc.nextInt();    // 첫 번째 숫자
        
        System.out.print("연산자를 입력해 주세요 : ");
        String b = sc.next();    // 연산자
        
        System.out.print("두 번째 숫자를 입력해주세요 : ");
        int c = sc.nextInt();    // 두 번째 숫자
        
        int num;    // 결과 값
        
        if(b.equals("+")) {    // 연산자가 더하기( + )일 경우
            num = a + c;
            System.out.println(a + " + " + c + " = " + num);
        }
        else if(b.equals("-")) {    // 연산자가 빼기( - )일  경우
            num = a - c;
            System.out.println(a + " - " + c + " = " + num);
        }
        else if(b.equals("*")) {    // 연산자가 곱하기( * )일 경우
            num = a * c;
            System.out.println(a + " * " + c + " = " + num);
        }else if(b.equals("/")) {    // 연산자가 나누기( / )일 경우
            num = a / c;
            System.out.println(a + " / " + c + " = " + num);
        }
        else {    // 값이 올바르게 입력되지 않았을 경우
            System.out.println("값을 올바르게 입력해주세요.");
        }
        
 
    }
 
}
 
cs

(실행 결과)

반응형