"Program 3"
Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its number of cubes and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.
1. Program Analysis: Use for loop control 100-999 number, each number decomposition out of bits, 10 bits, hundred.
Methods: A three-digit number, decomposed into bits, 10 bits, hundred, used to take the remainder of the operation, and then to obtain the three number of three times to add = = three digits
1 Private BooleanFlowernumber (intNumber ) {2 BooleanFlag =true;3 intone = number% 10;4 intTen = number/10% 10;5 inthundred = number/100% 10;6 Doublev = Math.pow (one, 3) + Math.pow (ten, 3) + Math.pow (hundred, 3);7 if(Number = = (int) (v)) {8Flag =true;9}Else {TenFlag =false; One } A returnFlag; -}
Test code:
1 @Test2 Public voidtest3 () {3 for(inti = 100; I <= 999; i++) {4 if(Flowernumber (i)) {5System.out.print (i+ ",");6 }7 }8 System.out.println ();9}
Test results:
153,370,371,407,
java-Basic Exercises 3