簡體   English   中英

需要幫助以隨機數1-10填充數組,而在Java中不使用0

[英]Need help getting populating array with random numbers 1-10 without using 0 in Java

需要幫助以隨機數1-10填充數組而不使用0。-創建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");




    }
}
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");
     }

我有一個業余的方式來做到這一點。

  1. 創建一個包含1-9的arraylist
  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);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM