728x90
1~100까지의 합(자바, while)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.doubles.javabasic.loops; public class JavaLoopWhile2 { public static void main(String[] args){ // while문 연습1 : 1~100까지의 합을 구해보자 int total = 0; // i의 값을 누적할 변수 선언 int i = 1; // i 초기값 1 선언 while(i <= 100){ // while문 조건식 : i는 100과 같아질 때 까지 반복 total = total + i; System.out.println("누적합계"+total); System.out.println(i); i++; // 1씩 증가시켜준다 } System.out.println("최종 합계"+total); } } | cs |