Two ways to print isosceles triangle Java! (depending on the number of rows, depending on the length of the bottom)

Source: Internet
Author: User

Let's start by judging the length of the bottom of the user input:

1  PackageCn.edu.nwpu.java;2 3 ImportJava.util.Scanner;4 5  Public classIsoscelestriangle {6 7      Public Static voidMain (string[] args) {8         //Print output isosceles triangle9System.out.println ("Please enter the length of the bottom of the isosceles triangle:");Ten          while(true) { OneScanner input =NewScanner (system.in); AString result =Input.next (); -             intNumber =integer.valueof (result); -             if(Number >= 3) { theSystem.out.println ("======= starts test ====== from here"); -                  for(inti = 1; I <= (number+1)/2; i++) { -                      for(intm = 1; M <= (number+1)/2-i; m++ ) { -System.out.print (""); +                     } -                      for(intj = 1; J <= 2*i-1; J + +) { +System.out.print ("*"); A                     } at System.out.println (); -                 } -}Else { -SYSTEM.OUT.PRINTLN ("Number cannot be less than 3, please re-enter:"); -             } -         } in     } -}

Take a look at the second method, enter the line number, the output triangle (this is a bit simpler):

1  PackageCn.edu.nwpu.java;2 3 ImportJava.util.Scanner;4 5  Public classIsoscelestriangle {6 7      Public Static voidMain (string[] args) {8         //Print output isosceles triangle9System.out.println ("Please enter the number of lines isosceles triangle:");Ten          while(true) { OneScanner input =NewScanner (system.in); AString result =Input.next (); -             intNumber =integer.valueof (result); -System.out.println ("======= starts test ====== from here"); the              for(inti = 1; I <= number; i++) { -                  for(intm = 1; M <= number-i; m++) { -System.out.print (""); -                 } +                  for(intj = 1; J <= 2 * I-1; J + +) { -System.out.print ("*"); +                 } A System.out.println (); at             } -         } -     } -}

Perform such as, this post of the classic in the first example, the inside of the law to think more headache!

Two ways to print isosceles triangle Java! (depending on the number of rows, depending on the length of the bottom)

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.