如何在java/android中重复任务?

如何在java/android中重复任务?,java,android,arrays,eclipse,string,Java,Android,Arrays,Eclipse,String,我想完成一项任务,为变量randomButton分配一个值。最后,我想检查randomButton的值,如果它是我不喜欢的,我想重新执行分配randomButton值的任务 我想做这个任务: int i = rng.nextInt(4); //Find random button from between 1-4 randomButton = buttons.get(i); buttonBackground = randomButton.getBa

我想完成一项任务,为变量
randomButton
分配一个值。最后,我想检查
randomButton
的值,如果它是我不喜欢的,我想重新执行分配
randomButton
值的任务

我想做这个任务:

        int i = rng.nextInt(4); //Find random button from between 1-4
        randomButton = buttons.get(i);
        buttonBackground = randomButton.getBackground();
        Log.v(TAG, "In the do");
如果

buttonBackground.equals(R.drawable.redcircle)
我想再次尝试做这项任务。我如何做到这一点

我试过:

do {
            int i = rng.nextInt(4); //Find random button from between 1-4
            randomButton = buttons.get(i);
            buttonBackground = randomButton.getBackground();
            Log.v(TAG, "In the do");
        }while(!buttonBackground.equals(R.drawable.redcircle));
但这是一个无限循环。那么,如果某个值是我不喜欢的,我如何重复一个任务呢


顺便说一下,
按钮
是一个具有不同图像按钮对象的arraylist。我想看看imagebutton(randomButton)的背景是否为红色圆圈。如果是,我想再看一遍代码。如果不是,那太好了,我可以继续。

执行while循环将是正确的方法。我不知道R.drawable.redcircle是什么意思,那是一种颜色吗

您在while子句中犯了错误:

}while(!buttonBackground.equals(R.drawable.redcircle));

我本应该在评论中发布这个,但我不能,因为我还没有50%的声誉。

一个do-while循环将是正确的方法。我不知道R.drawable.redcircle是什么意思,那是一种颜色吗

您在while子句中犯了错误:

}while(!buttonBackground.equals(R.drawable.redcircle));

我本应该在评论中发布这个,但我不能,因为我还没有50%的声誉。

将while设置为正确的值(你不喜欢的东西),并确保有时它可能是你喜欢的东西,因此现在它永远不是R.drawable.redcircle我想你的问题在于
按钮背景.equals(R.drawable.redcircle)
R.drawable.redcircle
是一个
int
。你到底想做什么?@PetterFriberg是的,我确信buttonBackground可以等同于可绘制的红圈之外的东西。我的按钮列表<代码>按钮包含四个具有不同绘图功能的按钮…因此有时是红色圆圈…你有!在此之前,它需要是R.drawable的int值。redcircle@PetterFriberg哦我只是想检查背景图像是否为红色圆圈。这就是为什么我把它放在这里!。说不平等。我应该怎么做呢?将while设置为正确的值(您不喜欢的东西),并确保有时它可能是您喜欢的东西,因此现在它不再是R.drawable.redcircle。我认为您的问题在于
按钮background.equals(R.drawable.redcircle)
R.drawable.redcircle
是一个
int
。你到底想做什么?@PetterFriberg是的,我确信buttonBackground可以等同于可绘制的红圈之外的东西。我的按钮列表<代码>按钮包含四个具有不同绘图功能的按钮…因此有时是红色圆圈…你有!在此之前,它需要是R.drawable的int值。redcircle@PetterFriberg哦我只是想检查背景图像是否为红色圆圈。这就是为什么我把它放在这里!。说不平等。我应该怎么做呢?澄清一下:
R.drawable
中的所有内容都是
drawable
类型的对象。它可以是任何颜色、图像、矢量图形、渐变,也可以是您可以在设备屏幕上绘制的任何东西。澄清一下:
R.drawable
中的所有内容都是
drawable
类型的对象。它可以是任何颜色、图像、矢量图形、渐变,或者简单地在设备屏幕上绘制的任何东西。