我遇到的问题是我的分数计数器变量'得分'在我的程序中不能高于1。如果用户输入的值等于显示的随机数(LoadG1),则会获得一个点。然后在最后输出,如较长的计时器所示。在较短的计时器中,添加了该点。 int变量在开头声明。全部如下所示。现在我知道它是最终的,这很可能是我的问题的原因,但IDE要求它是最终的,否则我只能调用变量'得分'在一个方法中(在我的情况下为CountDown Timer)。这给我带来了麻烦。如果用户每次都有正确的输入,我打算每次重复4秒计时器时添加一个点,但此刻它不能超过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();
如果有人可以提前解决我的问题,我将非常感激。感谢提前。
答案 0 :(得分:1)
因为您没有读取您生成的随机值,所以只读取第一个。所以第一次答案是正确的,但下次你回答不同,但是if
与第一次生成的随机数进行比较,所以它不算作有效答案(如果你输入相同的第一个数字,它将会计数)。由于存在当前编号,因此每次都需要从TextEdit编号中读取编号。
所以它可能是:
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));
// here we get from text field the current correct value
int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
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();
此外,您可以(并且应该)使用AtomicInteger而不是int[]
,您感兴趣的方法是:
int AtomicInteger#get();
int AtomicInteger#incrementAndGet()
所以将得分声明为AtomicInteger,如:
final AtomicInteger score = new AtomicInteger();
然后代替score[0]++;
做score.incrementAndGet();
然后当您阅读结果时:score.get();
答案 1 :(得分:1)
您是否在final int[] score = {0};
写了onCreate
?
尝试将int[] score = {0};
写出onCreate
作为全局变量。