这是我的计算器程序
/*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);
}
}
它在语句if (z = "Sum") {a = x + y;};
中显示错误“不兼容的类型”,我认为它必须有更多错误。我是初学者。我想让这个程序完美无误,所以它的工作方式与小型计算器完全相同。
答案 0 :(得分:0)
您可以使用'=='进行比较 (并打印结果而不是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);