我有4个按钮与答案,并希望消除其中一个按钮。我有一种随机选择按钮的方法,我使用if语句来确定所选按钮是否实际上是实际答案,因为我不希望该按钮被删除。问题是,有1/4的按钮被选中,所以如果被选中,我希望进程重新启动,直到if条件为false为止。
private int[] myInt;
public static final Random rgenerator = new Random();
public void onClickEliminate(View view) {
Resources res = getResources();
myInt = res.getIntArray(R.array.ansArray);
int ans = myInt[rgenerator.nextInt(myInt.length)];
Button button = (Button) findViewById(ans);
CharSequence message = button.getText().toString();
TextView textview = (TextView) findViewById(R.id.correct);
CharSequence answer = textview.getText().toString();
if (message.equals(answer)) {
// Here I need the above process to run again
Log.i(LOGTAG, "Keep this button");
} else {
button.setVisibility(View.GONE);
Log.i(LOGTAG, "Button gone");
}
}我意识到,这将需要在后台运行,但我只是看看目前的基本功能。有谁能帮上忙吗。你好,德里克
ps:我已经尝试过使用循环,但它们要么无效,要么无限。我认为,通过使用随机,“消息”的值应该在某个阶段发生变化。
发布于 2014-06-07 09:32:00
试试这个:
boolean myprocess = true;
while(myprocess){
if (message.equals(answer)) {
// Here I need the above process to run again
Log.i(LOGTAG, "Keep this button");
} else {
myprocess = false;
button.setVisibility(View.GONE);
Log.i(LOGTAG, "Button gone");
}
}https://stackoverflow.com/questions/24095386
复制相似问题