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