Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为所有按钮设置不同的随机数?_Java_Android_Button_Random - Fatal编程技术网

Java 如何为所有按钮设置不同的随机数?

Java 如何为所有按钮设置不同的随机数?,java,android,button,random,Java,Android,Button,Random,我在一个数组中有4个按钮。我能够生成一个随机数并将其设置为所有这些按钮,但这不是我想要做的。我真正想做的是给每个按钮设置一个随机数。例如:我可能在每个按钮中都有“18”、“15”、“10”和“11”,而不是在所有四个按钮中都有“17”。我不需要手动为每个按钮分配一个随机输入,如何才能对所有按钮执行此操作 这是我的密码: Random rbselection = new Random(); final int rbselector = rbselection.nextInt(4); final B

我在一个数组中有4个按钮。我能够生成一个随机数并将其设置为所有这些按钮,但这不是我想要做的。我真正想做的是给每个按钮设置一个随机数。例如:我可能在每个按钮中都有“18”、“15”、“10”和“11”,而不是在所有四个按钮中都有“17”。我不需要手动为每个按钮分配一个随机输入,如何才能对所有按钮执行此操作

这是我的密码:

Random rbselection = new Random();
final int rbselector = rbselection.nextInt(4);
final Button[] selectrb = new Button[4];
selectrb[0] = rb1;
selectrb[1] = rb2;
selectrb[2] = rb3;
selectrb[3] = rb4;
Random randoms1 = new Random();
int setRandoms1 = randoms1.nextInt(10);
for(int allrbA=0; allrbA<4; allrbA++) {
    selectrb[allrbA].setText(""+setRandoms1);
}
Random rbselection=new Random();
最终int-rbselector=rbselection.nextInt(4);
最终按钮[]选择RB=新按钮[4];
选择RB[0]=rb1;
选择RB[1]=rb2;
选择RB[2]=rb3;
选择RB[3]=rb4;
Random randoms1=新的Random();
int setRandoms1=randoms1.nextInt(10);
如果我是你的话,(int-allrbA=0;allrbA)

public static void main(String[] args) {
    Set<Integer> uniqueRandomNumbers = new LinkedHashSet<>();

    while (uniqueRandomNumbers.size() != 4) {
        Random random = new Random();
        uniqueRandomNumbers
                .add(Math.abs(Integer.valueOf(random.nextInt())));
    }
    System.out.println(uniqueRandomNumbers);
}
publicstaticvoidmain(字符串[]args){
Set uniqueRandomNumbers=new LinkedHashSet();
while(uniqueryandomnumbers.size()!=4){
随机=新随机();
uniquerandom数
.add(Math.abs(Integer.valueOf(random.nextInt())));
}
System.out.println(uniqueRandomNumbers);
}
说明: 我正在生成随机数。首先我得到了随机数100,我将其添加到“集合”中,因为集合始终保持唯一性,如果我再次得到100,则集合的大小不会增加

集合的大小为4时,循环中断,集合包含唯一的随机数


迭代
集合
以设置文本。

以获取Java中的随机数:

Random rand = new Random();

int  n = rand.nextInt(50) + 1;
其中1将是范围内的最小值,50将是最大值。请使用此链接作为参考:

取决于你有多少个按钮。你可以有

rbselection.nextInt(50) + 1;
每次调用时生成一个介于1到50范围之间的新整数,并将其添加到某个列表或集合中

比如说:

Random rand = new Random();
int  n = rand.nextInt(50) + 1;

ArrayList<Integer> ar = new ArrayList<Integer>();
int i = 0;

while (i < 4)
{    
    int temp = rbselection.nextInt(50) + 1;
    if (ar.contains(temp))
        continue;

    ar.Add(temp);
    i++;
}
Random rand=new Random();
int n=兰特nextInt(50)+1;
ArrayList ar=新的ArrayList();
int i=0;
而(i<4)
{    
int temp=rbselection.nextInt(50)+1;
如果(应力包含(温度))
继续;
ar.Add(温度);
i++;
}
此外,您可以将上述代码更改为:

while (i < 4)
{
    int temp = rbselection.nextInt(50) + 1;
    if (ar.contains(temp))
        continue;

    array[i].setText(temp);
    ar.Add(temp);
    i++;
}
while(i<4)
{
int temp=rbselection.nextInt(50)+1;
如果(应力包含(温度))
继续;
数组[i].setText(temp);
ar.Add(温度);
i++;
}
其中array是大小为4的按钮数组。

请尝试以下操作:

Random rnd = new Random();
for(int allrbA=0; allrbA<4; allrbA++)
    selectrb[allrbA].setText(String.valueOf(rnd.nextInt(20)));
Random rnd=new Random();

对于(int allrbA=0;allrbA)这如何保证每个按钮上有不同的随机数?