1

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

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 to continue 不接受,但第二次接受。谁能帮助解释发生了什么以及如何纠正它?

提前致谢!

编辑:

我将代码修改为:

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

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:

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

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

4

2 回答 2

5

因为一个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();
}
于 2013-03-14T16:50:50.917 回答
1

为什么不只是

while (reallyWantZero != 1 && reallyWantZero != 2) {
    System.out.println("enter 1 or 2");
    reallyWantZero = input.nextInt();
}

//do stuff here with that info
于 2013-03-14T16:55:20.743 回答