2015-08-29 48 views
-1

這裏是我的源代碼:錯誤:錯誤的操作數類型的二進制運算符「 - 」

/* package whatever; // don't place package name! */ 

import java.util.*; 
import java.lang.*; 
import java.io.*; 

/* Name of the class has to be "Main" only if the class is public. */ 
class Ideone 
{ 
public static void main (String[] args) throws java.lang.Exception 
{ 
    System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE); 
    System.out.println("Integer.MAX_VALUE + 1 = " + (Integer.MAX_VALUE + 1)); 
    System.out.println("Integer.MAX_VALUE * 2 = " + (Integer.MAX_VALUE * 2)); 
    System.out.println("Integer.MAX_VALUE * 5 = " + (Integer.MAX_VALUE * 5)); 
    System.out.println("Integer.MAX_VALUE * 10 = " + ( Integer.MAX_VALUE * 10)); 
    System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE); 
    System.out.println("Integer.MIN_VALUE - 1 = " + (Integer.MIN_VALUE - 1)); 
    System.out.println("Integer.MIN_VALUE * 2 = " + (Integer.MIN_VALUE * 2)); 
    System.out.println("Integer.MIN_VALUE * 5 = " + (Integer.MIN_VALUE * 5)); 
    System.out.println("Integer.MIN_VALUE * 10 = " + ( Integer.MIN_VALUE * 10)); 
    //Part 2 
    System.out.println("Integer.MAX_VALUE + 1.0 = " + (Integer.MAX_VALUE + 1.0)); 
    System.out.println("Integer.MAX_VALUE * 2.0 = " + (Integer.MAX_VALUE * 2.0)); 
    System.out.println("Integer.MAX_VALUE * 5.0 = " + (Integer.MAX_VALUE * 5.0)); 
    System.out.println("Integer.MAX_VALUE * 10.0 = " + ( Integer.MAX_VALUE * 10.0)); 
    System.out.println("Integer.MIN_VALUE - 1.0 = " + (Integer.MIN_VALUE - 1.0)); 
    System.out.println("Integer.MIN_VALUE * 2.0 = " + (Integer.MIN_VALUE * 2.0)); 
    System.out.println("Integer.MIN_VALUE * 5.0 = " + (Integer.MIN_VALUE * 5.0)); 
    System.out.println("Integer.MIN_VALUE * 10.0 = " + ( Integer.MIN_VALUE * 10.0)); 
    //Part 3 
    int a, b; 
    a = 1; 
    b = 2; 
    System.out.println("The ints a, b are " + a + ", " + b); 
    System.out.println("a + b is " + a + b); 
    System.out.println("a - b is " + a - b); 
    System.out.println("a * b is " + a * b); 
    System.out.println("a/b is " + a/b); 
    //Part 4 
    double aD, bD; 
    aD = 1.0; 
    bD = 2.0; 
    System.out.println("The doubles aD, bD are " + aD + ", " + bD ); 
    System.out.println("aD + bD is " + aD + bD); 
    System.out.println("aD - bD is " + aD - bD); 
    System.out.println("aD * bD is " + aD * bD); 
    System.out.println("aD/bD is " + aD/bD); 
} 
} 

這裏是我的錯誤:

Compilation error time: 0.1 memory: 320512 signal:0 
Main.java:37: error: bad operand types for binary operator '-' 
    System.out.println("a - b is " + a - b); 
             ^
first type: String 
second type: int 
Main.java:46: error: bad operand types for binary operator '-' 
    System.out.println("aD - bD is " +aD - bD); 
             ^
first type: String 
second type: double 
2 errors 

我是新來的Java,我仍然搞清楚算術。我以爲我做得很好,但我不明白出了什麼問題。這很可能是一個真正的菜鳥錯誤,但你能告訴我我做錯了什麼嗎?

回答

3
"a - b is " + (a) - (b)` 

指:

("a - b is " + (a)) - (b) 

。左邊部分(("a - b is " + (a)))是一個字符串,不能從字符串中減去。

你需要使用括號:

"a - b is " + (a - b) 
+1

謝謝!我只是面對如此艱難。 – WilRebl

0

+-考慮作爲字符串操作,其中從表達被評估從左到右。爲了避免這種情況紀念減法來在高優先級,當你與()

System.out.println("a - b is " + (a - b)); 

現在由於優先級的括號內(a-b)表達式求值,然後再整個表達式由左到右環繞其發生。

相關問題