这是我第一次用Java编程,所以我不明白为什么我不能访问函数外部的变量。我试图改变变量决赛,但还是有一个错误:如何访问Java内部类中的非最终变量号?
The final local variable number cannot be assigned, since it is defined in an enclosing type
当有在变量没有final关键字,错误是这样的:
Cannot refer to a non-final variable number inside an inner class defined in a different method
这里是我的代码:
String[] number = this.nextNumber();
Button submitbutton = (Button)findViewById(R.id.button1);
submitbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
//Toast.makeText(Addition.this, inp.getText(), Toast.LENGTH_SHORT).show();
if(inp.getText().toString().equals("")) {
Toast.makeText(Addition.this, "Please enter your answer", Toast.LENGTH_SHORT).show();
} else {
int answer = Integer.parseInt(inp.getText().toString());
if(Integer.parseInt(number[1]) == answer) {
Toast.makeText(Addition.this, "NICE!", Toast.LENGTH_SHORT).show();
int currentPoint = Integer.parseInt((String) points.getText());
currentPoint = currentPoint + 1;
points.setText(Integer.toString(currentPoint));
number = Addition.this.nextNumber(); // this part doesn't work. why?
} else {
Toast.makeText(Addition.this, "Wrong!", Toast.LENGTH_SHORT).show();
}
}
}
});
我对不起作用的部分发表评论。请告诉我为什么
我想要一个按钮被点击时重置的数量。有没有其他方式来改变我的按钮被点击时变量的值? –
@Kevin:但是,这是一个*本地*变量。你认为改变它会有什么影响? –