2014-09-23 96 views
-1

我正在Eclipse中製作一個計算器程序作爲excersize。我收到很多錯誤。我想要做的是改變兩個引用thisdec的「名稱」,以使該代碼稍後在該方法中更清晰。問題是什麼?無法在方法中創建其他實例引用

public Decimals plus(Decimals dec) { 
    //let's get the length of the 2 numbers 
    int this_len=this.getLength(), dec_len=dec.getLength(); 
    int longest_len; 
    //to simplify the code the two numbers are called long or short 
    Decimals short; //shortest <-- Syntax error on token 'short', ++ expected/Decimals cannot be resolved to a variable 
    Decimals long; //longest <-- Syntax error on token 'long', ++ expected/Decimals cannot be resolved to a variable 
    if (this_len >= dec_len){ 
     short = dec; // <-- Syntax error on token "=", delete this token/Duplicate local variable dec 
     long = this; // <-- Syntax error on token "long", invalid Expression 
     longest_len = this_len; 
    } 
    else { 
     short = this; // <-- Syntax error on token "short", VariableDeclaratorId expected after this token 
     long = dec; // <-- Syntax error on token "long", invalid Expression 
     longest_len = dec_len; 
    } 
+0

'short'和'long'是原語。不能使用基元作爲變量名稱 – noMAD 2014-09-23 20:56:24

+0

'short'和'long'是Java中的關鍵字。它們由語言保留,您不能將它們用作變量名稱。 – markspace 2014-09-23 20:56:29

回答

3

shortlong是關鍵字。您不能將它們用作變量名稱。 shortestlongest會很好。

+0

謝謝,我從未想過這件事 – maxpesa 2014-09-23 20:59:08

相關問題