본문 바로가기
IT/Java

Java (while문_정수의 합)

by hjshims 2021. 4. 9.

정수 1개를 입력받아 그 정수까지의 합을 출력

 

package p0409;
import java.util.Scanner;

public class WhileEx2 {
//정수 1개를 입력받아 그 정수까지의 합을 출력
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		
		int in=0;		//입력 변수
		int cnt=1;		//카운트 변수
		int sum=0;		//카운트 만큼 더 한 값을 저장하는 변수
				
		System.out.print("수를 입력하세요. ");
		in = scan.nextInt();
		
		while(cnt<=in) {
			sum += cnt;
			System.out.println(sum);
			cnt++;
		}
		
	}

}