在這個遊戲中,用戶每輸入一個等於產生的隨機數的整數(稱爲'LoadG1'的變量),他們就得分。如果他們的輸入== loadG1,他們會得到一個添加到'score'變量的點。每次他們回答正確時,應該增加1分,但由於某種原因,他們的得分不會超過1分。如果有一個問題是正確的,他們會得到一個顯示在最後的點。任何更正確的答案都會被忽略。在倒數計時器中得分不會更新
下面的代碼:
final int[] score = {0};
final Random generateG1 = new Random();
final int loadG1 = generateG1.nextInt(1000000)+10000;
final TextView number = (TextView) findViewById(R.id.number);
number.setText(" "+loadG1);
final CountDownTimer loop = new CountDownTimer(4000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.VISIBLE);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(loadG1));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score[0]++;
}
number.setVisibility(View.VISIBLE);
final int loadG1 = generateG1.nextInt(1000000) + 10000;
number.setText(" " + loadG1);
input.getText().clear();
start();
return true;
default:
}
}
return false;
}
});
}
}.start();
new CountDownTimer(24000, 1000) {
@Override
public void onTick (long millisUntilFinished) {
}
@Override
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score[0]);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.GONE);
loop.cancel();
}
}.start();
得分變量聲明之前任一計時器,則點在短4秒計時器溶液,並在較長的定時器的結束打印。全部顯示在上面的代碼中。
如果有人能夠幫助我確保每次輸入正確答案時都更新分數計數器,請提前致謝,我將不勝感激。
爲什麼所有yor變量都是'final'? – Kacper
@Kacper IDE需要它們中的很多才能讓我的程序成功編譯。 – User44
可能的重複[如何以特定的方式重複這個倒數計時器...?](http://stackoverflow.com/questions/40057754/how-can-i-repeat-this-countdown-timer-in-特定方式) – Selvin