簡體   English   中英

循環僅打印其他語句

[英]Loop Prints Else Statement Only

我正在制定各種數學培訓計划。 我正在嘗試實現一個for循環,以便用戶選擇的每個問題類型將生成三個問題。 如果用戶輸入正確的變量類型,這將非常有效。 但是,如果用戶輸入錯誤的變量/變量類型,將會發生奇怪的循環。 很難解釋,但是如果您采用了代碼並嘗試輸入錯誤的代碼,您很快就會看到。 我不確定這可能是怎么造成的,所以我想在這里發布。 我已注釋掉,因此僅一種操作類型(添加)可用,僅用於調試目的。 如果您鍵入的不是程序預期的內容,您將看到。 我對循環很陌生,並且在多種想法上都盡力了,但是我很快就提出了。 謝謝!

    // Welcome
    System.out.println("Hello and welcome to the Math Trainer!\n======================================");
    System.out.println("Which math operation would you like to practice?");

    // Print options to select from
    System.out.println("    " + "[A]ddition"); 
    System.out.println("    " +"[S]ubtraction"); 
    System.out.println("    " + "[M]ultiplication");
    System.out.println("    " + "[D]ivision");
    System.out.println("    " + "[R]emainder");

    // Ask for user input on which to choose
    System.out.print("Enter your choice:" + " ");
    String userLetter = stdin.nextLine();

    // Calculate random values from seed and shift them within range
        for(int count = 0; count < Config.NUMBER_OF_QUESTIONS; count++){
        int ran1 = randGen.nextInt(Config.MAX_VALUE - Config.MIN_VALUE + 1);
        int ran2 = randGen.nextInt(Config.MAX_VALUE - Config.MIN_VALUE + 1);
        int ran1Shift = ran1 + Config.MIN_VALUE;
        int ran2Shift = ran2 + Config.MIN_VALUE;

        // Initialize different answers per operation
        double additionAnswer = (double)ran1Shift + ran2Shift;
        double subtractionAnswer = (double)ran1Shift - ran2Shift;
        double multiplicationAnswer = (double)ran1Shift * ran2Shift;
        double divisionAnswer = (double)ran1Shift / ran2Shift;
        double remainderAnswer = (double)ran1Shift % ran2Shift;



    // Prompt user with a question based upon random numbers and operation selection
    // Presentation of addition problems
    if(userLetter.equalsIgnoreCase("a")) {
        System.out.print("What is the solution to the problem:" + " " + ran1Shift + " " + "+" + " " + ran2Shift + " = ");
        if (stdin.hasNextDouble()) {
            double userNum = stdin.nextDouble();
            if (userNum == additionAnswer) {
                System.out.println("That is correct!");
            } else {
                System.out.println("The correct solution is: " + additionAnswer + ".");
            }
        } else {
            System.out.println("All solutions must be entered as decimal numbers.");
            System.out.println("The correct solution is " + additionAnswer + ".");
            }
        }

    else{
    System.out.println("I'm sorry, I only understand choices of: A, S, M, D, or R!");
    }
}

    // Program exit
    System.out.println("======================================");
    System.out.println("Thank you for using the Math Trainer!");
}

}

例如,如果我輸入“ z”而不是a,s,m,d或r,程序將打印else語句3次。

假設3次是因為Config.NUMBER_OF_QUESTIONS為3,那是因為您執行了String userLetter = stdin.nextLine(); 循環之外 ,因此userLetter的值永遠不會改變。

如果您修復了代碼的縮進,那么for循環的范圍將變得很清楚,您將看到需要在循環內移動以下幾行:

// Ask for user input on which to choose
System.out.print("Enter your choice:" + " ");
String userLetter = stdin.nextLine();

原始答案

hasNextDouble()不會消耗任何東西,因此當您問一個問題,並且用戶響應時, I don't know而不是輸入數字,文本仍然存在。

然后,當您問下一個問題時,上一個問題的(錯誤)答案仍在緩沖區中,並且下一個hasNextDouble()調用也hasNextDouble()系統失敗,依此類推,等等,...

這是人們使用Scanner的主要缺陷之一。 他們忘記了錯誤處理。

在這種情況下,每行給出一個答案,因此,每當您閱讀答案時,都應始終之后調用nextLine() ,以在答案之后舍棄任何多余的文本,或者在輸入錯誤的情況下舍棄整行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM