The Question is:
Write, compile ad test a program that stores two integers and allow the user to enter a character. If the character is A, add the two integers, if it is S subtract the second integer from the first one and if it is M multiply the two integers. Display the arithmetic.
Problem: Although there is no compiling error or syntax error the above code is not working. Why is it so?
This is the output am getting in the Console window:
X: 2
Y: 3
Z: Invalid choice!
import java.util.*;
public class Calculate {
public static void main(String[] args) {
int x,y;
String z;
Scanner sn =new Scanner(System.in);
System.out.print("X: \t");
x=sn.nextInt();
System.out.print("Y: \t");
y=sn.nextInt();
System.out.print("Z: \t");
z=sn.nextLine();
if(z.equalsIgnoreCase("A")){
System.out.println("Sum: \t"+(x+y));
}
else{
if(z.equalsIgnoreCase("S")){
System.out.println("Subtraction answer: \t"+(y-x));
}
else{
if(z.equalsIgnoreCase("M")){
System.out.println("Multiplication answer: \t"+(x*y));
}
else{
System.out.println("Invalid choice!");
}
}
}
}
}