Java类和对象5

写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的立方和等于这个三位数本身,如:

371 = 33 + 73 + 13。)在主类E的main方法中来测试类Print。

 

 1 class sXianHuaShu {
 2     void outPut() {
 3         for (int i = 100; i <= 999; i++) {
 4             int gw = i % 10;
 5             int sw = i / 10 % 10;
 6             int bw = i / 100;
 7             if (i == gw * gw * gw + sw * sw * sw + bw * bw * bw) {
 8                 System.out.println(i);
 9             }
10         }
11     }
12 
13     public static void main(String[] args) {
14         sXianHuaShu shu = new sXianHuaShu();
15         shu.outPut();
16 
17     }

结果:

posted @ 2016-05-17 13:56  唐枫  阅读(165)  评论(0编辑  收藏  举报