Java计算器不执行if语句

参见英文答案 > Using scanner.nextLine()                                     5个
>            Scanner issue when using nextLine after nextXXX                 我对编程比较陌生,最近开始学习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).

我搜索了类似的问题,并尝试了他们的建议,但它似乎仍然没有用.我会感激任何帮助,我认为这可能只是我正在制作的一些简单错误,所以如果这看起来像一个愚蠢的问题,我道歉!

编辑:谢谢你的快速反应,伙计们!我很感激.是的,我修正了乘法和除法.

上一篇:科学记数法android java


下一篇:javascript – 如何计算数字和数学运算符的数组(或字符串)