-5
我是一個初學者編程(字面上兩天新),我有一個問題。我試圖製作一個非常基本的計算器,用戶輸入第一個數字,操作(僅限+, - ,*和/),然後輸入第二個數字。我很難得到操作的輸入以確定if語句的答案,所以有人可以用非常簡單的術語來解釋它嗎?有人可以解釋這一點,如果我已經去告訴我什麼是錯的聲明?
package learn;
import java.util.Scanner;
class calculator{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
double termOne, operation, termTwo, answer;
System.out.println("Enter first term: ");
termOne = input.nextDouble();
System.out.println("Enter operation (Valid operations are : ");
operation = input.nextDouble();
System.out.println("Etner second term: ");
termTwo = input.nextDouble();
if (operation == add) {
answer = termOne + termTwo;
System.out.println(answer);
} else if (operation == subtract) {
answer = termOne - termTwo;
System.out.println(answer);
} else if (operation == divide) {
answer = termOne/termTwo;
System.out.println(answer);
} else (operation == multiply) {
answer = termOne * termTwo;
System.out.println(answer);
}
}
}
哪裏有'add'及其朋友定義...? – Tunaki
它看起來像你要求字符串表示操作,但你正在閱讀雙倍,這是有點混淆。此外,如果您最終將閱讀字符串不使用'=='比較它。閱讀:[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo
@Pshemo [在JDK 7發行版中,您可以在 'public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg)中使用一個String對象String typeOfDay; switch(dayOfWeekArg){ case「Monday」: typeOfDay =「開始工作周」; 休息; case「Tuesday」: case「Wednesday」:'](https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html) –