[英]Why is my message printing twice when I break out of the inner if?
我的代碼有一點問題。 編譯和運行效果很好,但是,當我嘗試突破內循環時,
System.out.println("Type which category you want to add to.");
System.out.println("Homework, Classwork, Labs, Test, Quizzes, Midterm, Final");
當我只希望它打印一次時,上面的代碼將兩次打印到終端。
我感覺這是我的括號對齊方式的一個簡單錯誤,但是我很難弄清楚該如何做。 任何幫助將不勝感激!
import java.util.Scanner;
public class GetGrade {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int MAX = 15;
int[] homework = new int[MAX];
int[] classwork = new int[MAX];
int[] lab = new int[MAX];
int[] test = new int[MAX];
int[] quizzes = new int[MAX];
int[] midterm = new int[MAX];
int[] fin = new int[MAX];
int hwCount, clCount, labCount, testCount, quizCount, midCount, finCount;
double hwTotal, clTotal, labTotal, testTotal, quizTotal, midTotal, finTotal;
double grade = 0;
String selection = "";
System.out.println();
System.out.println("Welcome to GetGrade!");
System.out.println();
while (true) {
System.out.println("Type which category you want to add to.");
System.out.println("Homework, Classwork, Labs, Test, Quizzes, Midterm, Final");
selection = input.nextLine();
if (selection.equals("homework")) {
System.out.print("What percentange of your grade is homework? > ");
double hwPercent = input.nextDouble();
System.out.println("Now begin typing your grades. When you are finished, type -1.");
for (int i = 0; i < homework.length; i++) {
homework[i] = input.nextInt();
hwTotal = homework[i] * hwPercent;
grade += hwTotal;
if (homework[i] == -1) break;
}
}
}
}
}
它看起來很簡單:
調用input.nextInt(); 在您的內部循環中不包括換行符。 因此,您正在破壞內部循環,接收僅包含換行符的下一行-input.nextLine();中的字符; 這是您的“ -1 \\ n”行的其余輸入,並由於與“作業”不匹配而再次進入主循環。
嘗試將while循環中的條件變量設置為實際的布爾值,而不是true。
同樣,當您調用“ break”時,您只會脫離for循環。 如果此時將布爾變量重新分配為false,則將完全退出while循環。
在while循環結束之前,添加“是否要繼續?(是/否)”功能。
如果用戶輸入“ N”或其他,則執行另一個中斷。 這種中斷將使您擺脫while循環。
使代碼正常工作的簡單方法是更改
selection = input.nextLine();
至
selection = input.next();
next()
僅讀取字符串值(這是您在代碼中實際執行的操作),而不是彼得建議的newline
。
因此,當您讀取newline
時,while的額外迭代不會發生。
當您使用掃描儀從鍵盤上讀取一行時,它會讀取用戶鍵入並提交其輸入之前的所有內容,包括換行符。 因此,例如:
Type which category you want to add to.
Homework, Classwork, Labs, Test, Quizzes, Midterm, Final
>
如果鍵入“ homework”,然后按Enter,則實際輸入將變為“ homework \\ n”。 input.nextLine()
將掃描輸入,直到遇到第一個換行符'\\ n',它將消耗該字符,然后返回到該點為止的所有內容(即“作業”)。
您的問題是input.nextInt()
不會占用換行符,因此, while
循環開始另一回合時,輸入緩沖區中仍然存在換行符。
Now begin typing your grades. When you are finished, type -1.
> ...
> -1
=> User input is "-1\n"
-------------------------------
// Meanwhile, back in the code...
for (int i=0;i<homework.length;i++) {
homework[i] = input.nextInt(); // <--- This call consumes "-1" but leaves '\n'
hwTotal = homework[i] * hwPercent;
grade += hwTotal;
if (homework[i] == -1) break;
}
下一個對input.nextLine()
調用將占用該換行符,而輸入緩沖區為空。
while (true) {
System.out.println("Type which category you want to add to.");
System.out.println("Homework, Classwork, Labs, Test, Quizzes, Midterm, Final");
selection = input.nextLine(); // <--- This call consumes the leftover '\n' and returns the empty string
...
而且,由於“”不等於“作業”,因此while
循環又進行了大約input.nextLine()
,但是這次輸入緩沖區為空,因此對input.nextLine()
的調用的行為與您期望的一樣。
// selection is empty, so this condition fails and the program loops back around
if (selection.equals("homework")) {
...
有兩個簡單的解決方案。 您可以
Integer.parseInt(input.nextLine())
代替input.nextInt()
while
循環的結尾添加對input.nextLine()
的額外調用以消耗最后的換行符 第一個選項可能是最健壯的,如果它們沒有給您提供有效的整數作為輸入,則會獲得運行時錯誤的額外好處。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.