while循环
package com.summer.cn; /** * @author summer *1—100累加求和问题 */ public class Test041515 { public static void main(String[] args) { //初始值 int i = 1; int sum = 0; //条件判断 while(i<=100){ //循环体 sum+=i; //迭代 i++; } System.out.println(sum); } }
for循环
package com.summer.cn; /** * @author Summer * 1至100累加求和 */ public class Test041516 { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum+=i; } System.out.println(sum); } }