我是新來的android,我正在嘗試創建一個遊戲。這是一個非常簡單的猜數字遊戲。如果用戶猜測正確,我想更改正確答案的值。我不確定如何做到這一點。他是我創建的代碼:我可以更改最終int值嗎
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");
}}
}
});
}
如何更改correctAnswer?我不得不把它稱爲最終的,並且不能改變它的價值。
「我可以更改最終整數的值」 - 編號 – Maroun
聲明int值 - 全局** ** –
_「我被迫稱之爲最終」_你是如何被迫的? – Baby