Java:随机数对任何事物都是一样的 import java.util.Random; 穆奥班{ 公共静态void main(字符串[]args){ Random rand=新的Random(); System.out.println(“索引\t值”); int randnumb=1+rand.nextInt(11); int数组[]=新的int[5]; 数组[0]=randnumb; 数组[1]=randnumb; 数组[2]=randnumb; 数组[3]=randnumb; 数组[4]=randnumb; 用于(int计数器=0;计数器

Java:随机数对任何事物都是一样的 import java.util.Random; 穆奥班{ 公共静态void main(字符串[]args){ Random rand=新的Random(); System.out.println(“索引\t值”); int randnumb=1+rand.nextInt(11); int数组[]=新的int[5]; 数组[0]=randnumb; 数组[1]=randnumb; 数组[2]=randnumb; 数组[3]=randnumb; 数组[4]=randnumb; 用于(int计数器=0;计数器,java,arrays,random,Java,Arrays,Random,问题:每个元素都有相同的值,但我希望每个元素都有随机和不同的值。这是因为您指定了相同的值 import java.util.Random; class Moo { public static void main(String[] args) { Random rand = new Random(); System.out.println("Index\tValue"); int randnumb = 1 + rand.nextInt(



问题:每个元素都有相同的值,但我希望每个元素都有随机和不同的值。

这是因为您指定了相同的值

import java.util.Random;

class Moo {

    public static void main(String[] args) {
        Random rand = new Random();

        System.out.println("Index\tValue");
        int randnumb = 1 + rand.nextInt(11);
        int array[] = new int[5];

        array[0] = randnumb;
        array[1] = randnumb;
        array[2] = randnumb;
        array[3] = randnumb;
        array[4] = randnumb;

        for (int counter=0; counter < array.length; counter++)
            System.out.println(counter + "\t" + array[counter]);
    }

}
你需要做什么

array[0]=randnumb;
array[1]=randnumb;
array[2]=randnumb;
array[3]=randnumb;
array[4]=randnumb;
或者你可以用更好的方式来做

array[0]=1+rand.nextInt(11);
array[1]=1+rand.nextInt(11);
array[2]=1+rand.nextInt(11);
array[3]=1+rand.nextInt(11);
array[4]=1+rand.nextInt(11);

每次需要生成新的随机数时,都需要调用
nextInt()
。因此,请这样做:

for (int i : arr) {
     // do whatever you want with your values here.I'll just print them
    System.out.println(i);
}

您正在为randnumb赋值,并使用相同的值初始化数组元素。试试这个

Random rand = new Random();

System.out.println("Index\tValue");
// Don't need this anymore...
//int randnumb = 1+rand.nextInt(11);
int array[]= new int[5];

array[0]=1+rand.nextInt(11);
array[1]=1+rand.nextInt(11);
array[2]=1+rand.nextInt(11);
array[3]=1+rand.nextInt(11);
array[4]=1+rand.nextInt(11);
import java.util.Random;
穆奥班{
公共静态void main(字符串[]args){
Random rand=新的Random();
System.out.println(“索引\t值”);
int-randnub;
int数组[]=新的int[5];

对于(int j=0;j),问题的位置不在这里。您需要循环遍历数组并在每次为每个元素分配相同的数字时分配一个随机值,以便它们是相同的。
Random rand = new Random();

System.out.println("Index\tValue");
// Don't need this anymore...
//int randnumb = 1+rand.nextInt(11);
int array[]= new int[5];

array[0]=1+rand.nextInt(11);
array[1]=1+rand.nextInt(11);
array[2]=1+rand.nextInt(11);
array[3]=1+rand.nextInt(11);
array[4]=1+rand.nextInt(11);
import java.util.Random;

class moo{
public static void main(String[] args){

Random rand = new Random();

System.out.println("Index\tValue");
int randnumb; 
int array[]= new int[5];
for(int j=0;j<=4;j++)
{
    randnumb=1+rand.nextInt(11);
    array[j]=randnumb;
}

for(int counter=0;counter<array.length;counter++)
        System.out.println(counter +"\t"+array[counter]);

}