0

I'm writing a simple calculator program, the user inputs two integers and a symbol (+ - * etc) which represents the operation (add, subtract, multiply, etc) to be applied to the integers and the program calculates what they asked.

I have a do-while loop to continually ask for input if the input does not match the given symbols.

I've tried comparing them but I'm unsuccessful and very uncertain on what to do

CODE:

Scanner scan = new Scanner(System.in);

int num1, num2, sum, difference, product;

String a, b, c, userInput;

char choice, plus, minus, times;

System.out.print(First Integer: --> ");
num1 = scan.nextInt();

System.out.print(Second Integer: --> ");
num2 = scan.nextInt();

a = "+";
b = "-";
c = "*";

plus = a.charAt(0);
minus = b.charAt(0);
times = c.charAt(0);

do {
    System.out.print("Choose + - or *");
    userInput = scan.Next();

    choice = userInput.charAt(0);
} while(choice != '+' || choice != '-' || choice != '*');

switch(choice) {
     case 1:
     sum = num1 + num2;
     System.out.println("Sum is " + sum);
     break;

     case 2:
     difference = num1 - num2;
     System.out.println("Difference is " + difference);
     break;

     case 3:
     product = num1 ' num2;
     System.out.println("Product is " + product);
     break;
} 
2
  • 1
    Start by inserting ' marks and changing || for && in while(choice != '+' && choice != '-' && choice != '*') Commented Sep 7, 2020 at 2:16
  • It is a good idea to always paste the errors or exception stack traces you get into the question (as text). Commented Sep 7, 2020 at 2:35

1 Answer 1

2

There were some errors in your code, I have tried to rectify them. Take a look,

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int num1, num2, sum, difference, product;
        String userInput;
        char choice, plus, minus, times;

        System.out.println("First Integer: --> ") ;
        num1 = scan.nextInt();

        System.out.println("Second Integer: --> ") ;
        num2 = scan.nextInt();

        plus = '+';
        minus = '-';
        times = '*';

        do {
        System.out.println("Choose + - or *") ;
        userInput = scan.next();
        choice = userInput.charAt(0);
            
        }while(choice != '+' && choice != '-' && choice != '*');
        
        switch(choice) {
            case '+':
            sum = num1 + num2;
            System.out.println("Sum is " + sum);
            break;

            case '-':
            difference = num1 - num2;
            System.out.println("Difference is " + difference);
            break;

            case '*':
            product = num1 * num2;
            System.out.println("Product is " + product);
            break;
                 
            default :
                 System.out.println("Wrong Choice!");
                 break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have edited the answer for the same. @DawoodibnKareem
This is very helpful! However I noticed that when I entered - the first time it told me it was wrong, however when I entered it again it worked? I'm guessing it worked since the second prompt was in the loop unlike the first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.