【问题标题】:Need help getting populating array with random numbers 1-10 without using 0 in Java需要帮助使用随机数 1-10 填充数组而不在 Java 中使用 0
【发布时间】:2015-12-26 13:35:18
【问题描述】:

需要帮助在不使用 0 的情况下使用随机数 1-10 填充数组。 - 创建一个包含 100 个整数的数组。我试过int random = r.nextInt(High-Low) + Low;但这会忽略每个数字的数量。

我需要在我的作业中做些什么:

  • 用 1 到 10 范围内的随机数填充数组。 (不为零)
  • 确定数组中所有数字的平均值。
  • 计算 100 数组中十个数字中每一个数字的出现次数。通过使用第二个大小为 10 个整数的数组,并根据在 100 个整数数组中找到的重复项数递增数组的每个元素.


package arrays;

import java.util.Arrays;
import java.util.Random;

public class Intergers {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random r = new Random();

        // Create an array of 100 integers.
        int array[] = new int[100];

        int a = 0;

        // Populate the array with random numbers ranging from 1 to 10.
        while (a < 100)
        {
            int random = r.nextInt(10);
            array[a] = random;
            a++;
        }

        //calculate sum of all array elements
        int sum = 0;

         for(int i=0; i < array.length ; i++)
             sum = sum + array[i];

        //calculate average value
         double average = (double)sum/array.length;

        System.out.println("Array: " + Arrays.toString(array));
        // System.out.println("Sum: " + sum);
         //System.out.println("Array Length: " + array.length);
         System.out.println("Average value of array is: " + average);

         // Count the occurrence of each of the ten numbers in the array of 100
         int[] occurrences = new int[10];
         for (int b : array) {
                occurrences[b]++;
            }
         // System.out.println("Array: " + Arrays.toString(occurrences));



          System.out.println(1 + " appeared " + occurrences[0] + " times");
          System.out.println(2 + " appeared " + occurrences[1] + " times");
          System.out.println(3 + " appeared " + occurrences[2] + " times");
          System.out.println(4 + " appeared " + occurrences[3] + " times");
          System.out.println(5 + " appeared " + occurrences[4] + " times");
          System.out.println(6 + " appeared " + occurrences[5] + " times");
          System.out.println(7 + " appeared " + occurrences[6] + " times");
          System.out.println(8 + " appeared " + occurrences[7] + " times");
          System.out.println(9 + " appeared " + occurrences[8] + " times");
          System.out.println(10 + " appeared " + occurrences[9] + " times");




    }
}

【问题讨论】:

标签: java arrays random numbers


【解决方案1】:
int random = r.nextInt(10);

会给你一个介于 0 到 9 之间的伪随机 int。只需加 1 即可获得 1 到 10 之间的范围:

int random = r.nextInt(10) + 1;

您还必须调整对 occurrences 数组的处理,以考虑数组索引从 0 开始的事实:

     int[] occurrences = new int[10];
     for (int b : array) {
         occurrences[b-1]++;
     }

     for (int i = 0; i < occurences.length; i++) {
         System.out.println(i+1 + " appeared " + occurrences[i] + " times");
     }

【讨论】:

  • 这样做给了我 Thread [main] (Suspended (exception ArrayIndexOutOfBoundsException)) Intergers.main(String[]) line: 47 on 'occurrences[b]++;'并做'int High = 10;整数低 = 1; int random = r.nextInt(High-Low) + Low;'给我这个pastebin.com/EvvpREq6
  • @RyanMills 这是您处理occurrences 数组的方式的另一个问题。见编辑
【解决方案2】:

我有一种业余的方法。

  1. 创建一个包含 1-9 的数组列表
  2. 创建一个数组来存储 100 个整数
  3. 遍历存储数组并存储数组列表洗牌后的第一项

实现:

    ArrayList<Integer> a = new ArrayList<>();
    for (int i = 1; i < 10; i++) {
        a.add(i);
    }
    int[] store = new int[100];
    for (int i = 1; i < 100; i++) {
        Collections.shuffle(a);            
        store[i] = a.get(0);

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 2014-06-22
    • 2020-04-15
    相关资源
    最近更新 更多