0

I'm very new to Java (Just started this afternoon), and I've been trying to build a simple conditional calculator using switch statements.

The code I'm trying to make is quite simple. At first, ask for user's input (Statement), then the first number, and the second number.

The correct calculations will be made in accordance to the value of the input (If it's 'Sum', then it'll be first number + second number. 'Sub' then first number - second number,...).

import java.util.Scanner;

class Scalc{
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        //Variables//
        double fnum, snum, anum; //(First Number, Second Number, Answer Number)//
        String statement;

        //Input//

        System.out.println("Enter Statement: ");
        statement = input.nextLine();

        System.out.println("Enter First Number: ");
        fnum = input.nextDouble();

        System.out.println("Enter Second Number: ");
        snum = input.nextDouble();

        //Decision//

        switch (statement){

        case "Sum":
            anum = fnum + snum;
            System.out.println(anum);
            break;

        case "Sub":
            anum = fnum - snum;
            System.out.println(anum);
            break;

        case "Mul":
            anum = fnum * snum;
            System.out.println(anum);

        case "Div":
            anum = fnum / snum;
            System.out.println(anum);
            break;

        default:
            System.out.println("STATEMENT UNDEFINED");
            break;

        }
    }
}

Right now, though, it's stuck at 'Sub', for subtraction.

The programme runs normally, but when I input 'Mul', instead of multiplying the number, it subtracts instead. Same goes for 'Div'. Technically, the code seems to be stuck at subtraction and can't get any farther than that.

I'm still a noob at this so ...

Anyone?

5
  • 1
    It does not subtract the number unless you enter statement Sub. But its dividing the number as you are missing break after multipliction. Commented Dec 13, 2017 at 12:39
  • Small advice for the division: it is always necessary to raise the exception in the case where the division would be by zero. Commented Dec 13, 2017 at 12:40
  • There is no issue, I compiled and checked it. The only issue is "Mul" command both multiplies and divides due to missing break. Commented Dec 13, 2017 at 13:07
  • 1
    You need to know that "sub" is not the same as "Sub". Case matters. @foxdie is correct - you're missing a break in the Sub block. Commented Dec 13, 2017 at 13:09
  • That's strange, I'm using Eclipse at the moment, and even with break the results came out wrong. It's probably the IDE, thanks everyone! Commented Dec 13, 2017 at 13:17

1 Answer 1

4

You need to add break in under Mul case otherwise your next case statements will keep on executing until end of switch or break

 case "Mul":
    anum = fnum * snum;
    System.out.println(anum);
 break;
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, I checked it again and found it before your answer (Silly mistake), though the problem still persists. Even after I added a break, the result still turned out to be wrong (fnum - snum).
i don't think so , it's super stange , letme test it
@MatthewClifford i don't think so there's any issue , it's runs as expected but probably something wrong with your input , try statement.trim(), see see the ideone live demo
@MatthewClifford if you are using command line then make sure to recompile the code to see the changes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.