我在Eclipse中制作计算器程序作为excersize。我收到很多错误。我想做的是"更改名称"在两个引用this
和dec
中,以使代码在后面的方法中更加清晰。有什么问题?
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 :(得分:3)
short
和long
是关键字。您不能将它们用作变量名称。 shortest
和longest
没问题。