返回列表 發帖

while 迴圈 (二)

利用 while 迴圈, 計算100~200間所有偶數的總合.

  1. public class Hello
  2. {
  3.         public static void main(String[] args)
  4.         {   
  5.                 int i=100,sum=0;
  6.                 while(i<=200)
  7.                 {
  8.                         sum+=i;
  9.                         i+=2;
  10.                 }
  11.                 System.out.println(sum);
  12.         }
  13. }
複製代碼

TOP

  1. package bbs.istak;

  2. public class BB {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                 int i = 100;
  6.                 int j = 200;
  7.                 int total=0;
  8.                 while (i <= j) {
  9.                         if(i%2==0) {
  10.                                 total=total+i;
  11.                         }
  12.                         i++;
  13.                        
  14.                 }
  15.                 System.out.println(total);
  16.         }

  17. }
複製代碼

TOP

  1. package bbs.istak.org.tw;

  2. public class Main {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                  int i=100;
  6.              int j=0;
  7.                 while(i<=200)
  8.                 {
  9.                     j=j+i;        
  10.                     i=i+2;
  11.                 }
  12.                 System.out.println(j);
  13.         }

  14. }
複製代碼
寶寶心裡苦,但寶寶不說。

TOP

  1. package bn.tw;

  2. public class Main {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                 int i = 100;
  6.                 int total = 0;
  7.                 while (i <= 200) {
  8.                         i=i+2;
  9.                         total += i;
  10.                 }
  11.                 System.out.println(total);
  12.         }
  13. }
複製代碼

TOP

  1. public class Ch22{
  2.     public static void main(String args[])
  3.     {
  4.         int i=100;
  5.         int j=0;
  6.         while(i<=200)
  7.         {
  8.                 j=j+i;        
  9.                 i=i+2;
  10.         }
  11.         System.out.println(j);
  12.     }
  13. }
複製代碼

TOP

  1. package morris;

  2. public class Main {

  3.         public static void main(String[] args) {
  4.                 int a=100;
  5.                 int total=0;
  6.         while(a<=200)
  7.         {
  8.                 total=total+a;       
  9.                 a=a+2;
  10.         }
  11.         System.out.println(total);
  12.    }

  13. }
複製代碼

TOP

返回列表