Java计算器不执行if语句 [英] Java calculator not executing if-statement

查看:124
本文介绍了Java计算器不执行if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm relatively new to programming and have recently started learning Java in order to move into Android programming. I thought I would create a very simple calculator to practice, but it seems that my if statement doesn't work.

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)。

I am using the Eclipse IDE, and it runs fine until it asks for which operation to do. It will display the options but won't let me enter anything (I've been testing it with multiplying 5 by 2).

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

I searched for similar questions and tried what they suggested, but it still doesn't seem to work. I would appreciate any help, I assume this is probably just some simple error I am making, so I apologize if this seems like a silly question!

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

Thanks for the quick responses, guys! I appreciate it. And yes, I fixed the multiply and division. :)

推荐答案

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

The problem is that nextInt() doesn't consume (doesn't read) the new-line character (that you input when you press [Enter]). One way to solve this is calling nextLine() after each nextInt():

//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

Another way to solve this, would be reading the numbers with nextLine() (which returns a String) and then parsing it to a int:

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

您无需额外添加 nextLine()因为已经调用的 nextLine()消耗了换行符。

You won't need to add an extra nextLine() because the new-line character is being consumed by the nextLine() already called.

,如@sotondolphin建议的那样,您可能需要查看 * / 操作。

Also, as @sotondolphin suggested, you may want to check your * and / operations.

这篇关于Java计算器不执行if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆