我寫了這個代碼在主:在Java錯誤創建新的對象
if (startAmount>0) //create new cashier object with or without a start amount
Cashier newCashier = new Cashier(startAmount);
else Cashier newCashier = new Cashier();
,並得到一個編譯錯誤,第二行和第三行:
Multiple markers at this line
- Cashier cannot be resolved to a variable
- Syntax error on token "newCashier", delete
和:
Multiple markers at this line
- Cashier cannot be resolved to a variable
- Syntax error, insert "AssignmentOperator Expression" to complete
Assignment
- Syntax error, insert ";" to complete Statement
但是當我用括號寫下這樣的代碼時:
if (startAmount>0)//create new cashier object with or without a start amount
{
Cashier newCashier = new Cashier(startAmount);
}
else{ Cashier newCashier = new Cashier();}
它似乎沒問題,沒有編譯錯誤。 有人可以幫我理解爲什麼嗎?
哪個版本的Java和您使用的IDE? – GingerHead
刪除作業,反正你不使用變量'newCashier'。 – Augusto
版本:版本:Kepler Service Release 2 –