user3058983
user3058983

Reputation: 243

Java Switch Statement: creating a calculator error

I was wondering if anyone can see what is wrong with my code. It works except that the program is not acknowledging my switch statement - I searched lots of questions but as I am a novice I am clearly missing something.

import java.util.Scanner;
class Calmlr1 {
    public static void main(String[]args) {
        Scanner input = new Scanner(System.in);
        String anotherOption = "y", operatorOpt= "a";
        int no1=0, no2=0;
        double result= 0;
        System.out.println ("Welcome to the online calculator! Let's begin...");
        while (anotherOption.equalsIgnoreCase ("y")) {
            System.out.println ("Please enter your 1st number: ");
            no1 = input.nextInt();
            System.out.println ("Please confirm your operator:\n1 = +\n2 = - \n3 = *\n4 = /");
            operatorOpt = input.next ();
            System.out.println ("Please enter your 2nd number: ");
            no2 = input.nextInt();

            switch(no1) {
              case 1:
                result=no1+no2;
                break;
              case 2:
                result=no1-no2;
                break;
              case 3:
                result=no1*no2;
                break;
              case 4:
                result=no1/no2; 
              default: 
                result = 0 ;
                break;
            }
            System.out.println("Your total calculation is: "+result);
            System.out.println("Would you like to do another sum? y/n ");
            anotherOption=input.next();
        }
    }
}

Upvotes: 2

Views: 631

Answers (5)

Hugo Sousa
Hugo Sousa

Reputation: 1926

Your switch should be on the operatorOpt and not on no1.

Also, you're missing a break in the case 4. So, if you want to do a division, you'll get 0 as result.

The input from the user for operatorOpt should be done with input.nextLine(). Or, if you want to keep the same switch statement, with input.nextInt().

It should be like this:

switch(operatorOpt)
{
    case "+":
    result=no1+no2;
    break;
    case "-":
    result=no1-no2;
    break;
    case "*":
    result=no1*no2;
    break;
    case "/":
    result=no1/no2; 
    break;
    default: 
    result = 0 ;
    break;
}

Upvotes: 1

Shri Suresh
Shri Suresh

Reputation: 473

Your switch should be on the operatorOpt and not on no1.

You can use like this

switch(operatorOpt)
{
    case "+":
        result=no1+no2;
        break;
    case "-":
        result=no1-no2;
        break;
    case "*":
        result=no1*no2;
        break;
    case "/":
        result=no1/no2; 
        break;
    default: 
        result = 0 ;
        break;
}

Upvotes: 0

Vijay Dubey
Vijay Dubey

Reputation: 1

Your switch statement should be on "operatorOpt" and not on "no1" as you suppose to check the operator and based on that you want to do the calculation. However, you must use JDK1.7 to use String in Switch statement since previous versions of JDK do not support String Switch. Also, you should use "break" in case 4.

Upvotes: 0

jlewkovich
jlewkovich

Reputation: 2775

You should be using switch(operatorOpt). Right now you are switching on the first number.

You also need to change:

int operatorOpt= 0;

operatorOpt = input.nextInt();

That is, if you want to keep your switch statement the same. Please also see @Daniel Imms answer for an additional bug fix.

Upvotes: 3

Daniel Imms
Daniel Imms

Reputation: 50189

Try adding a break at the end of case 4

case 4:
result=no1/no2; 
break;

EDIT J L's answer is the main issue, but this is another problem that will break division.

Upvotes: 2

Related Questions