所以我仍然試圖讓這個計算器正常工作。整個觀點是它一直要求y值和一個操作員,直到用戶輸入「q」。唯一的是它不會返回值,它不會停止,一旦我輸入q。它只是在不斷詢問y值。下面是我繼續收到的輸出的一個例子。我是否必須創建一個額外的變量來保存y值以更新它並在之後將其打印出來?Java計算器不會輸出答案
X:5 Y:5 OP:+ Y:5運算:Q Y:
Scanner keyboard = new Scanner(System.in);
double x;
double y;
String Op;
System.out.print("X: ");
x = keyboard.nextDouble();
do{
System.out.print("Y: ");
y = keyboard.nextDouble();
keyboard.nextLine();
System.out.print("Op: ");
Op = keyboard.nextLine();
if(Op == "+"){
double result = sum(x,y);
System.out.println(result);
}
else if(Op == "/"){
double division = div(x,y);
System.out.println(division);
}
else if(Op == "-"){
double subtraction = sub(x,y);
System.out.println(subtraction);
}
else if(Op == "*"){
double times = mult(x,y);
System.out.println(times);
}
else if(Op == "q")
System.out.print("Your last result was: " +y);
}while(Op != "q");
}
public static double sum (double a, double b){
double res = a + b;
return res;
}
public static double div (double a, double b){
if(b==0){
System.out.println("Error, attempted a division by zero");
return b;
}
double divide = a/b;
return divide;
}
public static double sub (double a, double b){
double minus = a-b;
return minus;
}
public static double mult (double a, double b){
double times = a*b;
return times;
}
}
use'String.equals' ... – Reimeus