我想編寫一個程序,輸出一個像這樣的鑽石圖案:打印鑽石 - 無法輸入輸入?
*
***
*****
***
*
我試圖通過先得到它打印鑽石的上半部分開始。
我可以將'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");
}
}
}
謝謝!這工作。 – Smeaux
關於for-loops的任何建議? – Smeaux
我添加了另一個解決'for'循環的問題。 –