用java求水仙花数

 1 
 2 
 3  public   class  NarcissisticNumber 
 4  {
 5           public   boolean  narcissisticNumberMethod( int  i)
 6          {
 7                   int  unit = i/ 100 ,
 8                      decade = (i/ 10 )% 10 ,
 9                      hundreds = (i% 10 )% 10 ;
10                  
11                   if (i == (Math.pow(unit,  3 ) + Math.pow(decade,  3 ) + Math.pow(hundreds,  3 )))
12                           return   true ;
13                   else
14                           return   false ;
15          }
16          
17           public   static   void  main(String[] args)
18          {
19                  NarcissisticNumber number =  new  NarcissisticNumber();
20                  
21                   for ( int  i =  100 ; i <  1000 ; i++)
22                  {
23                           if (number.narcissisticNumberMethod(i))
24                                  System.out.println(i +  "是水仙花数"  +  " \t " );
25                  }
26                   /*int unit, decade, hundreds;
27                  
28                  for(int i = 100; i <= 999; i++)
29                  {
30                          unit = i/100;
31                          decade = (i/10)%10;
32                          hundreds = (i%10)%10;
33                          
34                          if(i == (Math.pow(unit, 3) + Math.pow(decade, 3) + Math.pow(hundreds, 3)))
35                          {
36                                  System.out.print("水仙花数为:" + i + "\t");
37                          }
38                  }
39                  */
40          }
41  }


你可能感兴趣的:(Java算法)