Skip to content
Advertisement

Simple calculator program in Java

I am a newbie coder in Java and I am trying to make this calculator in java where a user can enter two numbers and select the operation to be done on those numbers. However when the code comes to selecting the operator it skips the user input and the if statement and directly implements the else statement.

JavaScript

Advertisement

Answer

Add Calc.nextLine(); after n2 = Calc.nextInt(); to consume the line feed.

You are also not using else if so all those if conditions will be checked even if previous if already matched (resulting in your final else being executed as long as operator not '/').

In this case you should probably just use a switch block.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement