gpt4 book ai didi

java - 如何在嵌套循环中获取第一行和最后一列

转载 作者:行者123 更新时间:2023-12-01 19:47:58 25 4
gpt4 key购买 nike

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();
}

代码的输出是: output of code is :

但我想要这样的输出: enter image description here

所以我想我需要获取第一行和最后一列

如何做到这一点

最佳答案

请尝试这个

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();
}

关于java - 如何在嵌套循环中获取第一行和最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52453469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com