この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;
}
}