Java 生成 10 个随机整数,将它们存储在一个数组中,然后调用一个方法来显示该数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26831340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:22:08  来源:igfitidea点击:

Generate 10 Random Integers storing them in an Array and then calling a Method to display the Array

javaarraysrandom

提问by RifferRaffers

so i need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array.

所以我需要在 1-20 范围内生成 10 个随机整数,但我必须将它们存储在一个名为 numbers 的数组中。然后我必须调用一个名为 displayArray 的方法,它显示数组的内容,对于分配,我必须使用 for 循环来遍历数组。

The method header for the displayArray method is:

displayArray 方法的方法头是:

public static void displayArray(int[] array)

This is what I have done

这就是我所做的

public class RandomIntegers {

    static int numbers = 0;

    public static void displayArray(int[] array) {
         System.out.println(numbers + "Numbers Generated");
    }

}//end class

and

public class Random_Integers{

    public static void main(String[] args) {

        RandomIntegers[] numbers = new RandomIntegers[10];

        //Generates 10 Random Numbers in the range 1 -20
        for(int i = 0; i < numbers.length; i++) {
          numbers[i] = (int)(Math.random() * 20);

            RandomIntegers Numbers = new RandomIntegers();

            numbers[i] = Numbers;

        }//end for loop

        for (int i = 0; i < numbers.length; i++) {
            numbers Numbers = numbers[i];       

            Numbers[i].displayArray;        
            System.out.println();

        }//end for loop
  }//end main method
}//end class

An error appears on the lines

行上出现错误

Type mismatch cannot convert from int to RnadomIntegers

类型不匹配无法从 int 转换为 RnadomIntegers

numbers[i] = (int)(Math.random() * 20);

numbers cannot be resolved to a type

数字无法解析为类型

numbers Numbers = numbers[i];

Syntax error enter 'AssignmentOperator Expression' to complete expression

语法错误输入“AssignmentOperator 表达式”以完成表达式

Numbers[i].displayArray;

I realize I need to assign an instance of the RandomIntegers class to the slot in the array to fix the first problem but i don't know how, could someone show me how do to so

我意识到我需要将 RandomIntegers 类的一个实例分配给数组中的插槽来解决第一个问题,但我不知道如何,有人可以告诉我该怎么做

and i don't know how to fix the other 2 problems i'm only learning how to use java so could someone please guide me in the right direction

我不知道如何解决其他两个问题,我只是在学习如何使用 Java,所以有人可以指导我朝着正确的方向发展

采纳答案by NerosE

You only have to use a single for loop - like this:

你只需要使用一个 for 循环 - 像这样:

public static void main(String[] args) 
{
    int[] numbers = new int[10];       
    //Generates 10 Random Numbers in the range 1 -20
    for(int i = 0; i < numbers.length; i++) {
      numbers[i] = (int)(Math.random()*20 + 1);
    }//end for loop
    System.out.println("Numbers Generated: " + Arrays.toString(numbers));
}

回答by Mightian

you have created an array of type RandomIntegers use this it will work int[] numbers = new int[10];there is no problem with the code

您已经创建了一个 RandomIntegers 类型的数组,使用它可以正常工作int[] numbers = new int[10];,代码没有问题

回答by chiastic-security

To generate a random integer, you're best off using this:

要生成随机整数,最好使用以下方法:

Random rand = new Random();
numbers[i] = rand.nextInt(20)+1;

The rand.nextInt(20)call will give a random number from 0 to 19, so adding 1 makes it from 1 to 20.

rand.nextInt(20)调用将给出一个从 0 到 19 的随机数,因此加 1 使其从 1 到 20。

In practice, you don't need to create a new Random()every time; you'd put this

在实践中,您不需要Random()每次都创建一个新的;你会把这个

Random rand = new Random();

at the beginning of your loop, and then

在循环的开始,然后

numbers[i] = rand.nextInt(20)+1;

inside it.

在里面。

Since you've got several errors, I'd suggest you start again, write your code bit by bit, and check at each stage that it compiles and does what you want. For instance, start by printing a single random number out, and check that that works.

由于您有几个错误,我建议您重新开始,一点一点地编写代码,并在每个阶段检查它是否编译并执行您想要的操作。例如,首先打印一个随机数,然后检查它是否有效。