我正在尝试编写一个输出菱形图案的程序,如下所示:
*
***
*****
***
*
我首先尝试让它打印钻石的上半部分。
我可以在控制台中输入“totalLines”,但是当它提示输入“字符”时,我无法输入任何内容。为什么会发生这种情况?
我们在大部分作业中都使用了 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");
}
}
}