编程练习题

打印出 100~1000 范围内的所有 “水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个“水仙花数”,因为 153=1 的三次方+5的三次方+3 的三次方。

 

 1 public class    TestDemo(){
 2 public static void main(String args[]){
 3     int x,y, z;    
 4 for( int i =100,i<1000,i++){
 5       int x =i/100;
 6       int y = (i/10)%10;
 7       int z = i%10;            
 8         if(i == x*x*x+y*y*y+z*z*z){
 9           System.out.ptint(i+",");
10         }
11 }
12     }    
13 }

 

2、通过代码完成两个整数内容的交换。

 1 public class TestDemo(){
 2     public static void main (String args[]){
 3       int x = 10;
 4         int y= 20;
 5         int temp =x;
 6         x = y;
 7         y =temp;
 8 System.out.println("x=”+x);
 9 System.out.println("y=”+y);
10 }
11 }

3、 判断某数能否被 3,5,7同时整除。

 1 public class TestDemo {
 2 public static void main(String[] args) {
 3 int data = 105;
 1 public class TestDemo {
 2     public static void main(String args[]){
 3         int sum =0;
 4         int x =100;
 5         do{
 6             sum+=x;
 7             x++;
 8         }while(x<=200);
 9         System.out.println(sum);
10     }
11 }

 1 public class TestDemo {
 2     public static void main(String args[]){
 3         int sum =0;
 4         for(int x = 100;x<=200;x++){
 5         sum +=x;
 6         
 7         }
 8         System.out.println(sum);
 9     }
10 }

 

 4 if (data % 3 == 0 && data % 5 == 0 && data % 7 == 0) {
 5 System.out.println(data + "可以同时被3、5、7整除。");
 6 } else {
 7 System.out.println(data + "不可以同时被3、5、7整除。");
 8 }
 9 }
10 }

4、 编写程序,分别利用 while 循环、do„while 循环和 for 循环求出 100~200 的累加和。

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

 

posted @ 2019-02-26 19:07  一博之主  阅读(94)  评论(0)    收藏  举报