PackageItcast.feng;ImportJava.util.Scanner;//Requirements: Triangular digital array for output n=6//1//2 3//4 5 6//7 8 9// one by one// from//analysis: See positive triangle, think for loop format, outer loop control line, Inner loop control column, inner loop condition y<x;//Ideas://1. Define a counter, one output at a time, and the counter to accumulate 1. The value of this counter is the value we want to print.//2. The line number is the same as the number of elements in this line, so we can use the line number to control the inner loop//3. Output counter in inner loop//output line wrapping inside the outer loop Public classT1 { Public Static voidMain (string[] args) {intCount=1; for(intx=0;x<6;x++){ for(inty=0;y<=x;y++) {System.out.print (Count+ "\ T"); Count++; } System.out.println (); } //instead of outputting n rows n columns of triangular digital arrayScanner sc=NewScanner (system.in); System.out.println ("Please enter number n"); intn=Sc.nextint (); Count=1; for(intx=0;x<n;x++){ for(inty=0;y<=x;y++) {System.out.print (Count+ "\ T"); Count++; } System.out.println (); } }}
The output is:
Triangular digital Array for output n=6 (Java Basics Review)