1 package com.workprojects; 2 /** 3 * 使用while循环求出1-100的偶数之和 4 * 2019-06-28 5 * @author L 6 */ 7 8 public class Work062802 { 9 public static void main(String[] args) { 10 int num = 0; 11 int sum = 0; 12 while(num<=100) { 13 sum += num; 14 num += 2; 15 } 16 System.out.println("1-100的偶数之和为:" + sum); 17 } 18 }
使用do-while循环求出1-100的偶数之和
1 package com.workprojects; 2 /** 3 * 使用do-while循环求出1-100的偶数之和 4 * 2019-06-28 5 * @author L 6 * 7 */ 8 public class Work062803 { 9 public static void main(String[] args) { 10 int sum = 0; 11 int num = 0; 12 do { 13 sum += num; 14 num += 2; 15 }while(num<=100); 16 System.out.println("1-100的偶数之和为:" + sum); 17 } 18 }
使用for循环求出1-100的偶数之和
1 package com.workprojects; 2 /** 3 * 使用for循环求出1-100的偶数之和 4 * 2019-06-28 5 * @author L 6 * 7 */ 8 public class Work062804 { 9 public static void main(String[] args) { 10 int sum = 0; 11 for(int num = 0;num <=100;num+=2){ 12 sum +=num; 13 } 14 System.out.println("1-100的偶数之和为:" + sum); 15 } 16 }