2017-07-27 71 views
-1

我一直在不斷地得到這些錯誤一個簡單的計算器我一直試圖讓 信息: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; 
    } 

} 

感謝您的幫助球員,也對不起,如果我不應該我要發佈此,我是新。

+2

正如錯誤所示:'result = num1 + num2;'試圖添加兩個掃描儀,這是沒有意義的。你可能意思是'result = num1int + num2int;'。如果你以更有意義的方式命名你的變量,你可能會避免這個錯誤。 – assylias

+0

我想你的命名方案對你自己並沒有太大的幫助。您正在添加兩臺掃描儀(爲什麼在任何情況下都需要兩臺掃描儀?)。 – Jack

+0

嗯,你認爲'num1 + num2'是什麼意思,當'num1'和'num2'都是'Scanner'類型?也許你的意思是'num1int'和'num2int'?提示:'num1'不是引用掃描器的好變量名稱,因爲掃描器*不是*數字。另外,你不需要兩個不同的掃描儀... –

回答

3

num1num2不是數字,而是java.util.Scanner類型。

也許你會使用num1intnum2int,如下:

switch (Operation) { 
    case "addition": 
     result = num1int + num2int; 
     break; 
    case "subtraction": 
     result = num1int - num2int; 
     break; 
    case "multiplication": 
     result = num1int * num2int; 
     break; 
    case "division": 
     result = num1int/num2int; 
     break; 
} 

我建議更改名稱,以反映真實的類型如下:

Scanner scannerNum1 = new Scanner(System.in); 
Scanner scannerNum2 = new Scanner(System.in); 
float result = 0; 

System.out.println("What is your first number?"); 
int num1 = scannerNum1.nextInt(); 
System.out.println("What is your second number?"); 
int num2 = scannerNum2.nextInt(); 
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; 
} 

注:另外是沒有必要定義兩個掃描儀。一個就足夠了。

1

在代碼中,你有,你嘗試添加NUM1和NUM2一起行

result = num1 + num2; 

。由於變量名,這似乎是合理的,但是當你考慮這些線,就成了可笑:

Scanner num1 = new Scanner(System.in); 
Scanner num2 = new Scanner(System.in); 

您不能添加兩個掃描儀,是沒有意義的理論,它使更少的意義在實踐中。我假設你試圖做到這一點:

result = num1int + num2int; 

這在理論上是好的,但在嘗試做做num1int = 3和num2int = 4此操作,你會得到,而不是結果的結果= 0 = 0.75:

result = num1int/num2int; 

這是因爲整數除法,它總是舍入到0將強制轉換爲加倍,以避免這一點,像這樣的:

result = ((double) num1int)/num2int; 

我希望這有助於澄清你的問題。祝你好運!