我一直在不斷地得到這些錯誤一個簡單的計算器我一直試圖讓 信息:java的:在編譯模塊「學習」用一個簡單的計算器有麻煩,我一直試圖讓
發生錯誤Error:(24, 31) java: bad operand types for binary operator '*'
first type: java.util.Scanner
second type: java.util.Scanner
Error:(27, 31) java: bad operand types for binary operator '/'
first type: java.util.Scanner
second type: java.util.Scanner
Error:(21, 31) java: bad operand types for binary operator '-'
first type: java.util.Scanner
second type: java.util.Scanner
Error:(18, 31) java: bad operand types for binary operator '+'
first type: java.util.Scanner
second type: java.util.Scanner
Error:(16, 16) java: incompatible types: java.util.Scanner cannot be converted to int
這是我的代碼
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner Operation = new Scanner(System.in);
Scanner num1 = new Scanner(System.in);
Scanner num2 = new Scanner(System.in);
float result = 0;
System.out.println("What is your first number?");
int num1int = num1.nextInt();
System.out.println("What is your second number?");
int num2int = num2.nextInt();
System.out.println("What operation would you like to perform?");
switch (Operation) {
case "addition":
result = num1 + num2;
break;
case "subtraction":
result = num1 - num2;
break;
case "multiplication":
result = num1 * num2;
break;
case "division":
result = num1/num2;
break;
}
}
感謝您的幫助球員,也對不起,如果我不應該我要發佈此,我是新。
正如錯誤所示:'result = num1 + num2;'試圖添加兩個掃描儀,這是沒有意義的。你可能意思是'result = num1int + num2int;'。如果你以更有意義的方式命名你的變量,你可能會避免這個錯誤。 – assylias
我想你的命名方案對你自己並沒有太大的幫助。您正在添加兩臺掃描儀(爲什麼在任何情況下都需要兩臺掃描儀?)。 – Jack
嗯,你認爲'num1 + num2'是什麼意思,當'num1'和'num2'都是'Scanner'類型?也許你的意思是'num1int'和'num2int'?提示:'num1'不是引用掃描器的好變量名稱,因爲掃描器*不是*數字。另外,你不需要兩個不同的掃描儀... –