Triangular digital Array for output n=6 (Java Basics Review)

Source: Internet
Author: User

 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)

Related Article

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.