【问题标题】:Switch doesn't accept input until the second attemptSwitch 在第二次尝试之前不接受输入
【发布时间】:2013-03-03 03:49:15
【问题描述】:

我正在为一个班级做一个项目,并且可以使用一些帮助来弄清楚我的代码为什么会这样。该任务是提示用户输入两个整数并选择一个算术运算来对它们执行,同时避免“除以零”的情况。这是我的代码的一部分:

import java.util.Scanner;

public class Program3 
{

public static void main(String[] args) 
{

    double operandOne;
    double operandTwo;
    char newLine = '\n';
    Scanner input = new Scanner (System.in);

    //"input" is an object which calls for input from the keyboard.

        System.out.print("This program will request values for two operands ");
        System.out.println("and perform an arithmetic operation of your choice on them.");
        System.out.println(newLine + "Please enter a value for the first operand.");
        operandOne = input.nextDouble();

        System.out.println("Thank you.  Please enter a value for the second operand.");
        operandTwo = input.nextDouble();

        // This section explains what's going on to the user and requests input for the operands.

        if ( operandTwo == 0 )
        {

            System.out.print("You will not be able to perform division if the ");
            System.out.println("second operand is zero!");
            System.out.println(newLine + "Please choose an option:");
            System.out.println("Type 1 to select a new value for the second operand.");
            System.out.println("Type 2 to continue with a value of zero.");

            // You can't divide by zero!  Are you SURE you want to use that number?

            int reallyWantZero = input.nextInt();
            do
            {   
                System.out.println(newLine + "You must type either 1 or 2 and press enter.");
                reallyWantZero = input.nextInt();
            }

            while ((reallyWantZero < 1) || (reallyWantZero > 2));
            {
                switch (reallyWantZero)
            {
                case 1:
                    System.out.println(newLine + "Please enter a new value for the second operand.");
                    operandTwo = input.nextDouble();
                break;
                case 2:
                    System.out.println(newLine + "Okay, we will proceed.");
                    break;
            }
            }
        }

当我执行代码时,我得到以下信息:

"请为第一个操作数输入一个值。

1

谢谢。请为第二个操作数输入一个值。

0

如果第二个操作数为零,您将无法执行除法!

请选择一个选项:

键入 1 为第二个操作数选择一个新值。 键入 2 以零值继续。

2

您必须输入 1 或 2 并按 Enter。

2

好的,我们继续。”

不明白为什么第一次输入2继续不接受,第二次就接受了。任何人都可以帮助解释发生了什么以及我该如何纠正它?

提前致谢!

编辑:

我将代码修改为:

好的,我明白你的意思,但它似乎不起作用。我将代码修改为:

while ((reallyWantZero < 1) || (reallyWantZero > 2));
                {
                    System.out.println(newLine + "You must type either     1 or 2 and press enter.");
                    reallyWantZero = input.nextInt();
                }               
                switch (reallyWantZero)
                {
                    case 1:

但它的行为仍然完全相同。另外,如果我输入了一个无效的数字,程序就会挂起并且根本不返回任何内容。

但它的行为仍然完全相同。另外,如果我输入了一个无效的数字,程序就会挂起并且根本不返回任何内容。

【问题讨论】:

    标签: java while-loop switch-statement do-while


    【解决方案1】:

    因为do...while 循环总是至少执行一次

    do
    {   
        System.out.println(newLine + "You must type either 1 or 2 and press enter.");
        reallyWantZero = input.nextInt();
    } 
    while ((reallyWantZero < 1) || (reallyWantZero > 2));
    

    它在执行一次循环体后检查条件。所以它总是输入这部分代码,这意味着用户将被要求至少输入两次。您需要使用 while 循环,它会在进入之前检查循环的条件。

    例如

    while ((reallyWantZero < 1) || (reallyWantZero > 2)) {
        System.out.println(newLine + "You must type either 1 or 2 and press enter.");
        reallyWantZero = input.nextInt();
    }
    

    【讨论】:

    • 哇,你的反应真快! :) 我会在问题中添加修改。谢谢。
    • 去掉while语句后的分号看看我上面的例子
    • 哈,就是这样!它现在按预期工作。非常感谢您的帮助。
    【解决方案2】:

    为什么不直接

    while (reallyWantZero != 1 && reallyWantZero != 2) {
        System.out.println("enter 1 or 2");
        reallyWantZero = input.nextInt();
    }
    
    //do stuff here with that info
    

    【讨论】:

    • 我是一个完整的初学者,所以我会在进行过程中弄清楚这一切。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多