2017-04-22 79 views
-1

这里是我的计算器程序如何纠正这个计算器程序?

/*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

你可以使用 '==' 比较 (并打印结果的不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); 
+0

谢谢user3804188 – bhavik13