[0628] use while, do-while, for loop to achieve the even and between 1 and 100

 

. 1  Package com.workprojects;
 2  / ** 
. 3  * using a while loop and obtaining an even number of from 1 to 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 is          the while (NUM <= 100 ) {
 13 is              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 }

 

Guess you like

Origin www.cnblogs.com/yanglanlan/p/11110856.html