我是Android的新手,我正在尝试创建一个游戏.这是一个非常简单的猜数字游戏.如果用户猜对了,我想改变正确答案的值.我不确定该怎么做.他是我的创建代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button subm = (Button) findViewById(R.id.button1); final TextView tv1 =(TextView) findViewById(R.id.textView1); final EditText userG=(EditText)findViewById(R.id.editText1); Random rand=new Random(); final int correctAnswer=rand.nextInt(100)+1; subm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int userNum=Integer.parseInt(userG.getText().toString()); if(userNum>100 || userNum<0){ tv1.setText("Enter a number between 1 and 100"); } else{ if(userNum==correctAnswer){ tv1.setText("YEAH! You got it right"); } else if(userNum>correctAnswer){ tv1.setText("Sorry,Your guess is too high"); } else if(userNum如何更改correctAnswer?我被迫称之为最终,不能改变价值.
Can i change value of final int??
对于这个答案是否定的..
我可以理解你在匿名内部类中使用它的问题所以eclipse强烈要求你让它成为最终的..所以你想correctAnswer
在匿名内部类中使用值,所以删除final
并定义你的correctAnswer全局像..
private int correctAnswer;
然后你可以改变值,你可以在匿名内部类中访问它