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)