Notice
Recent Posts
Recent Comments
«   2024/10   »
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
Archives
Today
Total
관리 메뉴

Cafe Binary Notation

[백준 알고리즘 1001번] A-B (자바) 본문

Java/알고리즘

[백준 알고리즘 1001번] A-B (자바)

켄달지나 2018. 2. 4. 22:28

단계별로 풀기: 1단계-3




문제:


첫째 줄에 A와 B가 주어진다. (0< A,B < 10)




나의 답안:


1
2
3
4
5
6
7
8
9
10
11
12
import java.util.*;
 
public class Main {
    public static void main(String[] args){
        
        Scanner scan = new Scanner(System.in);
        int num1 = scan.nextInt();
        int num2 = scan.nextInt();
        
        System.out.println(num1-num2);
    }
}
cs





위의 답안으로 제출해도 백준님는 맞았다고 해주지만 

(0< A,B < 10)이라는 조건이 있으므로

두 수 중 하나라도 (0< A,B < 10) 조건을 만족하지 않으면 재입력을 받도록 구현해 보았다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
 
public class Main {
    public static void main(String[] args){
        
        Scanner scan = new Scanner(System.in);
        
        int num1 = scan.nextInt();
        int num2 = scan.nextInt();
   
        while(num1<=0 || num1>=10 || num2<=0 || num2>=10) {
            
            System.out.println("입력받는 수는 0과 10사이여야 합니다.");
            num1 = scan.nextInt();
            num2 = scan.nextInt();            
        }
        
        System.out.println(num1-num2);
    }
}
 
cs