有谁知道为什么当我运行这个 java 应用程序时,它不断出现默认值,而不是正确的答案?
主要代码如下:
import java.util.Scanner;
public class mainClass {
public static void main(String[] args){
Scanner scanInt = new Scanner(System.in);
Scanner scanString = new Scanner(System.in);
System.out.println("Calculator");
System.out.println("Enter 1st number:");
int x = scanInt.nextInt();
System.out.println("Enter opperation:");
String op = scanString.nextLine();
System.out.println(op);
System.out.println("Enter 2nd number:");
int y = scanInt.nextInt();
String ans = "The answer is: ";
if(op == "+"){
System.out.println(ans + methods.addition(x, y));
}else if(op == "-"){
System.out.println(ans + methods.subtraction(x, y));
}else if(op == "*"){
System.out.println(ans + methods.multiplication(x, y));
}else if(op == "/"){
System.out.println(ans + methods.division(x, y));
}else{
System.out.println("UNKNOWN OPPORATOR");
}
}
}
方法类如下:
public class methods {
public static int subtraction(int x, int y){
return x - y;
}
public static int addition(int x, int y){
return x + y;
}
public static int multiplication(int x, int y){
return x * y;
}
public static int division(int x, int y){
return x / y;
}
}