while循環案例


 

 1 class While05{  2     public static void main(String[ ]args){  3         //練習1:使用while循環完成輸出1------10中的每個數
 4         /*int i =1;  5  while(i<=10){  6  System.out.println(i);  7  i++;  8  }*/
 9  } 10 } 11 
12 class While06{ 13     public static void main(String[ ]args){ 14         //練習2:使用while循環完成輸出所有的兩位數
15         int i =10; 16         while(i<=99){ 17             if(i %2 !=0){ 18  System.out.println(i); 19  } 20             i++; 21  } 22         //練習3:使用while循環完成輸出50---100范圍內所有的奇數 23         
24 
25         //練習3: 26         //第一種方法:
27         /*int i = 51; 28  while(i<=100){ 29  System.out.println(i); 30  i +=2; 31  }*/
32 
33         //第二種方法:
34         /*int i = 51; 35  while(i <=100){ 36  if(i %2 !=0){ 37  System.out.println(i); 38  } 39  i++; 40  }*/
41  } 42 } 43 
44 class While07{ 45     public static void main(String[ ]args){ 46     //練習4:使用while循環完成輸出所有三位數中能被4整除的數,並且每行顯示5個
47     
48         int i = 100,count = 0; 49         while(i <=999){ 50             if(i % 4 == 0){ 51                 System.out.print(i + "\t"); 52                 count ++; 53  } 54             i++; 55             if(count % 5 ==0){ 56  System.out.println(); 57  } 58  } 59  } 60 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM