我正在尝试为我的第一个java项目创建一个计算器。我向用户询问两个数字输入和一个符号来选择操作。它要求输入一旦到达if语句就停止计算答案。
import java.util.Scanner;
class Calculator{
public static void main(String args[]){
double a;
double b;
double answer;
String symbol;
Scanner in = new Scanner(System.in);
System.out.print("Enter an operation");
symbol = in.nextLine();
System.out.print("Enter the first number");
a = in.nextInt();
System.out.print("Enter the second number");
b = in.nextInt();
if (symbol == "+"){
answer = a+b;
System.out.print(answer);
}
else if (symbol == "-"){
answer = a-b;
System.out.print(answer);
}
else if (symbol == "/"){
answer = a/b;
System.out.print(answer);
}
else if (symbol == "*"){
answer = a*b;
System.out.print(answer);
}
}
}
这是我的输出
Enter an operation +
Enter the first number 4
Enter the second number 5