【问题标题】:Java calculator not executing if-statement [duplicate]Java计算器不执行if语句[重复]
【发布时间】:2014-02-06 07:19:53
【问题描述】:

我对编程比较陌生,最近开始学习 Java,以便进入 Android 编程领域。我以为我会创建一个非常简单的计算器来练习,但是我的 if 语句似乎不起作用。

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        //Create new scanner object
        Scanner numInput = new Scanner( System.in );

        //Enter first number
        System.out.println("Please enter the first number: ");
        int num1 = numInput.nextInt();

        //Enter the second number
        System.out.println("Please enter the second number: ");
        int num2 = numInput.nextInt();

        //Choose the operation to perform (+,-,*,/)
        System.out.println("What operation would you like to do?");
        System.out.println("Type \"+\" to add.");
        System.out.println("Type \"-\" to subtract.");
        System.out.println("Type \"*\" to multiply.");
        System.out.println("Type \"/\" to divide.");
        String opChoice = numInput.nextLine();


        //Add
        if (opChoice.equals("+")) {
            int ans = num1 + num2;
            System.out.println("Adding " + num2 + " to " + num1 + " equals " + ans + ".");
        }

        //Subtract
        else if (opChoice.equals("-")) {
            int ans = num1 - num2;
            System.out.println("Subtracting " + num2 + " from " + num1 + " equals " + ans + ".");
        }

        //Multiply
        else if (opChoice.equals("*")) {
            int ans = num1 + num2;
            System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + ".");
        }

        //Divide
        else if (opChoice.equals("/")) {
            int ans = num1 + num2;
            System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + ".");
        }

    }

}

我正在使用 Eclipse IDE,它运行良好,直到它询问要执行哪个操作。它会显示选项,但不允许我输入任何内容(我一直在用 5 乘以 2 对其进行测试)。

我搜索了类似的问题并尝试了他们的建议,但它似乎仍然不起作用。我会很感激任何帮助,我认为这可能只是我犯的一些简单错误,所以如果这看起来是一个愚蠢的问题,我深表歉意!

编辑:感谢您的快速回复,伙计们!我很感激。是的,我修复了乘法和除法。 :)

【问题讨论】:

  • 我还没有遇到的有趣问题。你每天都能学到新东西!

标签: java if-statement calculator


【解决方案1】:

问题在于nextInt() 不消耗(不读取)换行符(您在按 [Enter] 时输入的)。解决此问题的一种方法是在每个 nextInt() 之后调用 nextLine()

//Enter first number
System.out.println("Please enter the first number: ");
int num1 = numInput.nextInt();
numInput.nextLine(); // Add this

//Enter the second number
System.out.println("Please enter the second number: ");
int num2 = numInput.nextInt();
numInput.nextLine(); // Add this

解决此问题的另一种方法是使用nextLine() 读取数字(返回String),然后将其解析为int

int num1 = Integer.parseInt(numInput.nextLine());

您不需要添加额外的nextLine(),因为换行符正在被已调用的nextLine() 占用。

此外,正如 @sotondolphin 所建议的,您可能需要检查您的 */ 操作。

【讨论】:

  • 您的解决方案已解决,谢谢!我会在 5 分钟内接受你的回答。 ;)
【解决方案2】:

问题是当numInput.nextInt(); 被调用时,你输入了数字......但它离开了换行符(\n)。您对numInput.nextLine(); 的调用会得到一个空字符串。

将调用替换为 numInput.next() 将解决问题,因为它的行为略有不同:

公共字符串 next()

从这个扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是匹配分隔符模式的输入。

默认分隔符模式是空格,其中包括\n,输入操作后输入流中的内容(以* 为例)现在是\n*\n

【讨论】:

    【解决方案3】:

    下面的代码做加法,但不是预期的乘法和除法。你能查一下来源吗?

    //Multiply
        else if (opChoice.equals("*")) {
            int ans = num1 + num2;
            System.out.println("Multiplying " + num2 + " with " + num1 + " equals " + ans + ".");
        }
    
        //Divide
        else if (opChoice.equals("/")) {
            int ans = num1 + num2;
            System.out.println("Dividing " + num1 + " by " + num2 + " equals " + ans + ".");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多