-1

Here is my calculator program

/*program calculator */
import java.util.Scanner;
class calculator

     { public static void main (String args[])
    {   System.out.println("First enter two number , then type Sum for addition, Sub for  subtraction, Mul for multiplication, Div for division");
        int x, y;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 1st number");
        x = in.nextInt();
               System.out.println("Enter 2nd number");
        y = in.nextInt();
               System.out.println("Enter Sum for addition, Sub for  subtraction, Mul for multiplication, Div for division");
        String z = in.next();
        if (z = "Sum") {a = x + y;};
        if (z = "Sub") {a = x - y;};
        if (z = "Mul") {a = x * y;};
        if (z = "Div") {a = x / y;};
       System.out.println("Result of entered two number = "+z);  
    }
} 

It is showing an error "incompatible types" in statement if (z = "Sum") {a = x + y;}; and I think it must have more mistakes. I am a beginner. I would like to make this program perfect with no errors, so it work exact same like a small calculator.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

you might use '==' for comparison (and print the result a not z)

if (z.equals("Sum")) {a = x + y;};
if (z.equals("Sub")) {a = x - y;};
if (z.equals("Mul")) {a = x * y;};
if (z.equals("Div")) {a = x / y;};
System.out.println("Result of entered two number = " + a);  
user3804188
  • 130
  • 6