사용자로부터 입력 받은 정수를 계속 더합니다.
사용자가 0을 입력하면 합을 출력합니다.
프로그램을 종료합니다.
Scanner sc = new Scanner(System.in);
int result = 0;
while (true) { //반복문//
System.out.println("정수를 입력해주세요(0을 입력시 프로그램 종료): ");
int num = sc.nextInt();
result += num; //변수를 하나더 선언해준뒤 그값(result)에 입력받은 num을 계속 합산//
if (num == 0)//num이 0일경우 break로 while문을 빠져나가 ↓아래에 리절트값을 출력//
break;
}
System.out.println("총 합산은 " + result + " 입니다.");
sc.close();
'java > java exercise' 카테고리의 다른 글
Print multiplication tables in vertical direction (0) | 2020.01.27 |
---|---|
Identify x and y are in the rectangle zone (0) | 2020.01.25 |
Generate multiplication table of a given number (0) | 2020.01.25 |
It will thank you until you're satisfied. (0) | 2020.01.25 |
Calculate and display Student Grades (0) | 2020.01.25 |