Java BASIC Programming questions

Source: Internet
Author: User

Java BASIC Programming Questions

1. Print out the case

1  Public classprog19{2      Public Static voidMain (string[] args) {3         intn = 5;4 Printstar (n);5         }6     7     //Print stars8     Private Static voidPrintstar (intN) {9         //print top HalfTen          for(inti=0;i<n;i++){ One              for(intj=0;j<2*n;j++){ A                 if(j<n-i) -System.out.print (""); -                 if(J>=n-i && j<=n+i) theSystem.out.print ("*"); -                 } - System.out.println (); -             } +         //print the lower half of the section -          for(inti=1;i<n;i++){ +System.out.print (""); A              for(intj=0;j<2*n-i;j++){ at                 if(j<i) -System.out.print (""); -                 if(J>=i && j<2*n-i-1) -System.out.print ("*"); -                 } - System.out.println (); in             } -         } to}
View Code

Problem-Solving ideas: first the graph into two parts to look at, the first five elements a rule, after four lines a rule, the use of double for Loop, the first layer of control row, the second Level control column. The important thing is to find the right rules!

2. title: Seeking 1+2!+3!+...+20! and the

1  Public classsuibian{2      Public Static voidMain (string[] args) {3         Longsum = 0;4          for(inti=0;i<20;i++) {5Sum + = factorial (i+1);6         }7 System.out.println (sum);8         }9     //factorialTen     Private Static LongFactorial (intN) { One         intmult = 1; A          for(inti=1;i<n+1;i++) -Mult *=i; -         returnmult; the         } -}
View Code

Program Analysis: Set factorial another function to avoid errors!

Java BASIC Programming questions

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.