Scanner scanner = new Scanner(System.in);
System.out.print("Masukkan nilai : ");
int input = scanner.nextInt();
int kolom = input -1;
for(int i=1;i<input;i++){
for(int j=input;j>i;j--){
System.out.print("*");
}
System.out.println();
}
所以我认为我需要获得第一行和最后一列
该怎么做
答案 0 :(得分:3)
请尝试这个
Scanner scanner = new Scanner(System.in);
System.out.print("Masukkan nilai : ");
int index = scanner.nextInt();
for (int i = 0; i < index; i++) {
System.out.print("*");
}
System.out.println();
for (int i = 1; i < index; i++) {
for (int j = index; j > i; j--) {
if (j == index || j == i + 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
答案 1 :(得分:0)
for(char c='*',i=0; i<input; c=' ',i++)
for(int j=0; j<input-i; j++)
System.out.print(j==0 ? "*" : j<input-i-1 ? c : "*\n");