Java Beginner Practice Answer (Loop)

Source: Internet
Author: User

/* The title is as follows: 1 (for Loop) * Programming to find the number of four-bit integer ABCD that satisfies the following relationship. (AB+CD) (AB+CD) =ABCD2 (loop) * read into an integer n, output such as shape n = 3 * ********n = 4 * * * * * * * * * * * * * * * * * * * * * * * * *, the number of loops in the outer loop. For each line, the thing to do: 1. Output a number of spaces; 2. Output a number of stars; 3. Line break. Hint: output does not wrap, with System.out.print () 3. (cycle) * * To find the number of daffodils. The so-called Narcissus number, refers to a three-digit ABC, if the A3+B3+C3=ABC is satisfied, then ABC is the number of daffodils. 4. (Loop) * * Enter an integer to calculate the and of the numbers on each of them. (Note: Is an integer of any bit) space: n-1 Descending asterisk: 1 3 5 2 3 4*/package Mytest;import Java.util.scanner;public class Chapunitone {//First answer Pub LIC void Answerone () {int A,b,c,d;/*int i = 1234;a = I/1000;b = i/100% 10;c = i/10% 10;d = i% 10; System.out.println (a); System.out.println (b); System.out.println (c); System.out.println (d); */for (int i = 1000;i<9999;i++) {a = I/1000;b = i/100% 10;c = i/10% 10;d = i% 10;int AB = A*10+b;int cd = C*10+d;int ad = ab+cd;if (Ad*ad = = i) {System.out.println (i);}}} Second answer public void Answertwo () {Scanner num = new Scanner (system.in); System.out.println ("Please enter an integer"); int mynum = Num.nextint (); for (int i=0; i<mynum; i++) {for (int j=0; j<=mynum-i-1; J + + ) {System.out.print (" ");} for (int j=0; j<=i*2; J + +) {System.out.print ("*");} System.out.println ();}} The third answer to public void Answerthree () {for (int i = 100;i<999;i++) {int a = i/100% 10;int B = i/10% 10;int c = i% 10;if ( A*a*a+b*b*b+c*c*c = = i) {System.out.println ("Number of Daffodils:" +i);}} Question fourth answer public void Answerfour () {Scanner num = new Scanner (system.in); System.out.println ("Please enter an integer:"); int mynum = Num.nextint (); int sum = 0;/* int a = i/100% 10;     int b = I/10% 10; int c = i% 10;*/system.out.print ("Sum ="), while (mynum>0) {int temp = Mynum%10;sum + = Mynum%10;mynum = Mynum/10;if (Myn Um!=0) System.out.print (temp+ "+"); ElseSystem.out.print (temp);} System.out.println ("=" +sum);} public static void Main (string[] args) {Scanner i = new Scanner (system.in); System.out.println ("Please enter the question number you want to see the answer:"); int num = I.nextint (); Chapunitone myanswer = new Chapunitone (); switch (num) {case 1:myanswer. Answerone (); Break;case 2:myanswer. Answertwo (); Break;case 3:myanswer. Answerthree (); Break;case 4:myanswer. Answerfour (); break;}}}

  

Java Beginner Practice Answer (Loop)

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.