Algorithm/백준알고리즘
[백준알고리즘] 반복문 13단계 -10951번 A+B - 4(JAVA)
개발자하소서
2022. 6. 21. 08:38
728x90
반응형
SMALL
📌문제
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
📌입력
입력은 여러 개의 테스트 케이스로 이루어져 있다.
각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)
📌출력
각 테스트 케이스마다 A+B를 출력한다.

*** has next() *****
hasNext뒤로 Boolean, Double, Int 등등 자료형이 붙은 건 입력된 값이 그 자료형일 경우에는 true값을 반환하고
아닐 경우 false를 반환한다.
예를 들어 hasNextInt() 값을 출력할 때 정수가 들어오면 true,
입력이 없거나 정수 외의 값이 들어올 경우 false를 반환한다.
📌코드
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
// 입력받는 값이 있는 지 확인하는 방법은
// hasnext(), hasnextInt()
int a=sc.nextInt();
int b= sc.nextInt();
System.out.println(a+b);
}
}
}
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while((str=br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(str);
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
System.out.println(a + b);
}
}
}
728x90
반응형
LIST