I am new to android and I am trying to create a game. It is a very simple Guess the number game.I want to change the value of the correct answer if he user guesses correctly. I am unsure how to do this. his is my code for on create:
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){
tv1.setText("Sorry,Your guess is too low");
}}
}
});
}
How do I change correctAnswer? I am forced to call it final and can't change the value.
Can i change value of final int??
For this answer is NO..
I can understand your problem you are using it in anonymous inner class so eclipse forcefully ask you to make it final..So you want to use correctAnswer
value within anonymous inner class so remove the final
and define your correctAnswer globally like ..
private int correctAnswer;
then you can change value and you can access it in anonymous inner class
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다