-8

Help guys I am trying to create a program that lets you choose +,-,/,* and how can I make it work using switch statement

import java.util.Scanner;

public class practice1{
    public static void main (String[]Args){
        Scanner input = new Scanner(System.in);
        Integer num1, num2;
        char op = '+', '-', '/', '*';

        System.out.println("Select an operator +,-,*,/");
        switch(op){
            case "+":
            System.out.println("Enter two numbers to add");
            System.out.print("Enter first number: "); num1 = input.nextInt();
            System.out.print("Enter second number: "); num2 = input.nextInt();
            System.out.println(num1 + num2);
            break;


        }

    }
}
Qwertyozz
  • 3
  • 4
  • 2
    What's the problem? Just as a few more `case`es. – Steve Smith Jul 14 '17 at 11:39
  • Look at how switch statements work, then add the extra cases. Simple! Then if you're feeling adventurous, look at replacing switch statements with polymorphism. – byxor Jul 14 '17 at 11:40
  • 3
    The problem is this line: `char op = '+', '-', '/', '*';`. That's not valid syntax. What do you intend to do on that line? – Andy Turner Jul 14 '17 at 11:43
  • 3
    You asked the user to choose an operator, but you haven't let him enter his choice... – Kevin Anderson Jul 14 '17 at 11:43
  • 1
    Another problem is that you are using `'+'` in one place and `"+"` in another. They are different types (`char` and `String`). May I suggest that you methodically work through the Oracle Java tutorial (or a good textbook). That's a better way to learn the language than ... guesswork + asking on StackOverflow. – Stephen C Jul 14 '17 at 12:23
  • Possible duplicate of [Reading a single char in Java](https://stackoverflow.com/questions/3043306/reading-a-single-char-in-java) – Tom Jul 14 '17 at 12:43

1 Answers1

0

Try out this :

package com.sujit;

import java.util.Scanner;

public class UserInput {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean flag = true;

        do {
        System.out.println("Enter 1st number");
        int num1 = input.nextInt();
        System.out.println("Enter 2nd number");
        int num2 = input.nextInt();

        System.out.println("select one operator :\n 1)+\n2)-\n3)*\n4)/\n5)Exit(Enter E)\n");
        System.out.println("Enter your choice :");

        char choice = input.next().charAt(0);
        String ch = String.valueOf(choice);
        switch (ch) {
        case "+":   
            System.out.println("Addition = "+(num1+num2));
            break;
        case "-":   
            System.out.println("Subtraction = "+(num1-num2));
            break;
        case "*":   
            System.out.println("Multiplication = "+(num1*num2));
            break;
        case "/":   
            if(num2==0){
                System.out.println("Cant devide by 0");
                flag=false;
            }
            else {
                System.out.println("Division = "+(num1/num2));
            }

        break;
        case "E":   
            input.close();
            flag=false;
            break;
        default:
            System.out.println("Wrong choice");
            break;
        }

        }       
        while(flag);
    }

}
sForSujit
  • 987
  • 1
  • 10
  • 24