我有一个程序,其中随机值应用于四个随机按钮。其中一个随机值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保持相同的数字。
非常感谢提前。
答案 0 :(得分:1)
在您的代码中,您正在重新初始化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);
}
}
});
}
现在你的代码应该可以工作了。