java-Basic Exercises 3

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.