我有一个程序,其中将随机值应用于四个随机按钮。显示在四个按钮顶部的随机值id之一。如果用户点击包含显示的数字的按钮(loadG4),他们将获得一个分数。我的问题是,一旦用户单击正确的按钮并生成了新的数字,rbvalue按钮可能就不会全部与loadG4值不同。从我的代码中可以看出,它们永远不应该相等。这是代码:
int score = 0;
Random random = new Random();
int rbselector = random.nextInt(4); //These four initiations are out onCreate.
int loadG4 = random.nextInt(10);
final Button[] selectrb = new Button[4];
selectrb[0] = rb1;
selectrb[1] = rb2;
selectrb[2] = rb3;
selectrb[3] = rb4;
final Button loseStarter4 = (Button) findViewById(R.id.Starter4);
loseStarter4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
infoG4.setVisibility(View.GONE);
loseStarter4.setVisibility(View.GONE);
rb1.setVisibility(View.VISIBLE);
rb2.setVisibility(View.VISIBLE);
rb3.setVisibility(View.VISIBLE);
rb4.setVisibility(View.VISIBLE);
rbselector = random.nextInt(4);
final TextView number = (TextView) findViewById(R.id.number);
number.setText(""+ loadG4);
for(int allrbA=0; allrbA<4; allrbA++) {
int rbvalue = random.nextInt(9);
if (rbvalue == loadG4) {
rbvalue=9;
}
selectrb[allrbA].setText(""+rbvalue);
}
selectrb[rbselector].setText(""+ loadG4);
for (int allrbA = 0; allrbA < 4; allrbA++) {
selectrb[allrbA].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button clicked = (Button) v;
String clickVal = (String) clicked.getText();
int finalClick = Integer.valueOf(clickVal);
if (finalClick == loadG4) {
score++;
for (int allrbA = 0; allrbA < 4; allrbA++) {
int rbvalue = random.nextInt(9);
if (rbvalue == loadG4) {
rbvalue=9;
}
selectrb[allrbA].setText("" + rbvalue);
}
int loadG4 = random.nextInt(10);
number.setText("" + loadG4);
int rbselector = random.nextInt(4);
selectrb[rbselector].setText("" + loadG4);
}
}
});
}
我拥有它,以便如果rbvalue等于loadG4,则将其设置为=9。由于某些原因,在第一次正确单击之后,rbvalue可能等于loadG4。
此外,loadG4的值在这里也不会改变。为什么是这样?
将感谢所有能够浏览并解释为什么a)第一次正确单击后rbvalue并不总是与loadG4不同以及b)为什么loadG4保持不变的原因。
提前谢谢了。
最佳答案
在您的代码中,您正在重新初始化loadG4和rbselector。相反,您应该使用全局变量。
//Declare this globally. Not in oncreate.
int score = 0;
Random random = new Random();
int rbselector = random.nextInt(4);
int loadG4 = random.nextInt(10);
final Button[] selectrb = new Button[4];
selectrb[0] = rb1;
selectrb[1] = rb2;
selectrb[2] = rb3;
selectrb[3] = rb4;
final Button loseStarter4 = (Button) findViewById(R.id.Starter4);
loseStarter4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
infoG4.setVisibility(View.GONE);
loseStarter4.setVisibility(View.GONE);
rb1.setVisibility(View.VISIBLE);
rb2.setVisibility(View.VISIBLE);
rb3.setVisibility(View.VISIBLE);
rb4.setVisibility(View.VISIBLE);
rbselector = random.nextInt(4);
final TextView number = (TextView) findViewById(R.id.number);
number.setText(""+ loadG4);
for(int allrbA=0; allrbA<4; allrbA++) {
int rbvalue = random.nextInt(9);
if (rbvalue == loadG4) {
rbvalue+=1;
}
selectrb[allrbA].setText(""+rbvalue);
}
selectrb[rbselector].setText(""+ loadG4);
for (int allrbA = 0; allrbA < 4; allrbA++) {
selectrb[allrbA].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button clicked = (Button) v;
String clickVal = (String) clicked.getText();
int finalClick = Integer.valueOf(clickVal);
if (finalClick == loadG4) {
score++;
loadG4 = random.nextInt(10); //removed declaration
for (int allrbA = 0; allrbA < 4; allrbA++) {
int rbvalue = random.nextInt(9);
if (rbvalue == loadG4) {
rbvalue+=1; // better method to prevent duplicates for 9
}
selectrb[allrbA].setText("" + rbvalue);
}
number.setText("" + loadG4);
rbselector = random.nextInt(4);
selectrb[rbselector].setText("" + loadG4);
}
}
});
}
现在您的代码应该可以工作了。