java loop

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:47:44
you will be creating a small Java program that will use a user validation loop to get a number in the range of 1 to 9, inclusive. Then the program will generate a triangular shaped figure on the screen, based on the user's input.
The triangular shaped figure for an input of 1 is:
1
The triangular shaped figure for an input of 2 is:
1
21
The triangular shaped figure for an input of 3 is:
1
21
321
The triangular shaped figure for an input of n is:
1
21
321
.
.
.
n ... 1

一楼的兄弟可能没理解意思把
public class Test1 {
public static void main(String[] args) throws IOException{
String s = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input 'exit' for exit !");
do {
System.out.println("The triangular shaped figure for an input of is: ");
s = br.readLine() ;
Integer i = new Integer(s);
print(i);
} while (!br.readLine().equals("exit"));
}

public static void print(int n) {
int x = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; j > 0; j--) {
if (j == i) {
x = j;
} else {
x--;
}
for (; x < n; x++) {
System.out.print(" ");
}
System.out.print(j);
}
System.out.println();
}
}
}结果:
The triangular shaped figure f