Java calculator not executing if-statement [duplicate]

馋奶兔 提交于 2019-12-19 22:04:00

问题


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 + ".");
        }

    }

}

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!

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


回答1:


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

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());

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

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




回答2:


The issue is that when numInput.nextInt(); is called, you get the number entered ... but it leaves the newline (\n). Your call to numInput.nextLine(); then gets an empty string.

Replacing that call with numInput.next() will solve the problem as it has a slightly different behavior:

public String next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

The default delimiter pattern is whitespace, which includes \n and what's in the input stream after you enter the operation (using * as example) is now \n*\n




回答3:


The code below does addition but not expected multiplication and division. Could you please check the source?

//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 + ".");
    }

}


来源:https://stackoverflow.com/questions/21177801/java-calculator-not-executing-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!