根据comments数组生成随机数

时间:2016-05-05 15:56:12

标签: java arrays random

我在编写这段代码时遇到了问题,我觉得我整个过程都很糟糕。

有人能指出我正确的方向吗?这是我到目前为止所拥有的

public class FArray {
    // build an array of 10 numbers of random numbers, and then display them out
    public static void main(String[] args) {
        // declare an array (named numbs) that holds 10 ints
        int[] numbs = new int[10];
        // write a for loop that loops 10 times and generates a random number between 0 and 100
        for (int i = 0; i < numbs.length; i++) {
            numbs[i] = (int) (Math.random() * 100 + 1);

            // assign the random number to each element in the array , numbs
            numbs = new numbers();
            // write a for loop that displays to the screen each random number in the array named numbs
            numbers[i] = Numbers;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果你只想要randomely打印10个数字,你可以使用整数流来完成,在这个流中我们从0到10迭代,对于每个整数,我们打印一个0到100之间的随机双精度:

IntStream.range(0, 10).forEach(i -> System.out.println(Math.random() * 100 + 1));

编辑:如果您只想将10个随机值插入数组麻木中,然后打印它们,则只需删除这两行:

        numbs = new numbers();
        // write a for loop that displays to the screen each random number in the array named numbs
        numbers[i] = Numbers;

所以你的主要方法看起来像这样:

  public static void main(String[] args) {
    // declare an array (named numbs) that holds 10 ints
    int[] numbs = new int[10];
    // write a for loop that loops 10 times and generates a random number between 0 and 100
    for (int i = 0; i < numbs.length; i++) {
       numbs[i] = (int) (Math.random() * 100 + 1);
    }
    // print the values
    for (int number : numbs) {
       System.out.println(number);
    }
 }