我有代碼需要輸入,然後計算出你想要用它做什麼Java - 解析簡單計算器程序的字符串的問題
例如。你會輸入「x(+, - ,.. etc)y」,它會爲你計算它。
即時通訊目前使用掃描儀和分裂它使得
double x = input.nextDouble();
String z = input.next();
double y = input.nextDouble();
現在我遇到了一個問題。說我想做一個階乘,我會輸入「x!」但代碼仍然需要最後一次input.nextDouble();
我該怎麼去(使用我在做的事情,如果可能的話)檢查是否所有3都有輸入,然後在使用if語句的方法之間進行選擇,或者只有2個輸入。
相對碼
System.out.print("> ");
double x = input.nextInt();
String z = input.next();
double y = input.nextInt();
if (x == 0) {
running = false;
} else if (z.equalsIgnoreCase("+")) {
System.out.println(addition(x, y));
}
您可以檢查運算符是一元運算符還是二元運算符。如果操作員對單個操作數進行操作,則需要單個輸入。如果操作員是二進制的,則需要兩個輸入。你將不得不檢查操作員哪個用戶想要做什麼..然後採取輸入。希望它可以幫助你。 – Coder
爲什麼0不是有效的第一個參數? – kukido