首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从if else语句中重新运行代码

如何从if else语句中重新运行代码
EN

Stack Overflow用户
提问于 2014-06-07 08:34:19
回答 1查看 778关注 0票数 0

我有4个按钮与答案,并希望消除其中一个按钮。我有一种随机选择按钮的方法,我使用if语句来确定所选按钮是否实际上是实际答案,因为我不希望该按钮被删除。问题是,有1/4的按钮被选中,所以如果被选中,我希望进程重新启动,直到if条件为false为止。

代码语言:javascript
运行
复制
    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:我已经尝试过使用循环,但它们要么无效,要么无限。我认为,通过使用随机,“消息”的值应该在某个阶段发生变化。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-07 09:32:00

试试这个:

代码语言:javascript
运行
复制
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");
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24095386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档