gpt4 book ai didi

java - 打印钻石 - 无法输入?

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:08 25 4
gpt4 key购买 nike

我正在尝试编写一个输出菱形图案的程序,如下所示:

   *
***
*****
***
*

我首先尝试让它打印钻石的上半部分。

我可以在控制台中输入“totalLines”,但当它提示输入“character”时我无法输入任何内容。为什么会发生这种情况?

我们在大多数作业中都使用 JOptionPane,因此我在这方面遇到麻烦是有道理的,但从我从阅读本书中可以看出,这是正确的。

(如果您有时间与我讨论 for 循环,我很确定它们需要改进。我将非常感激。)

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int totalLines, lines, currLine = 1, spaces, maxSpaces, minSpaces, numCharacters, maxCharacters, minCharacters;

String character;

System.out.print("Enter the total number of lines: ");
totalLines = input.nextInt();
System.out.print("Enter the character to be used: ");
character = input.nextLine();

lines = ((totalLines + 1)/2);
// spaces = (Math.abs((totalLines + 1)/2)) - currLine;
maxSpaces = (totalLines + 1)/2 - 1;
minSpaces = 0;
// numCharacters = (totalLines - Math.abs((totalLines +1) - (2*currLine)));
maxCharacters = totalLines;
minCharacters = 1;
spaces = maxSpaces;

for (currLine = 1; currLine<=lines; currLine++) {
for (spaces = maxSpaces; spaces<=minSpaces; spaces--){
System.out.print(" ");
}
for (numCharacters = minCharacters; numCharacters>= maxCharacters; numCharacters++){
System.out.print(character);
System.out.print("\n");
}
}


}

最佳答案

尝试使用 next() 而不是 nextLine()

关于java - 打印钻石 - 无法输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16681428/

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