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