2015-09-29 30 views
0

需要幫助以隨機數字填充數組1-10不使用0. - 創建一個由100個整數組成的數組。我試過int random = r.nextInt(High-Low) + Low;但是會拋出每個數字有多少個數。需要幫助以隨機數字1-10填充數組而不使用Java中的0

我需要在我的任務做:

  • 填充陣列隨機數爲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"); 




    } 
} 
+0

「但是,拋出每個數字有多少」 - 以什麼方式? (請記住,你需要'高 - 低+ 1'或者你需要'高'來獨家...) –

+0

這已經回答了:[http://stackoverflow.com/questions/363681/generating-random-整數範圍與java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) –

回答

4
int random = r.nextInt(10); 

會給你一個僞隨機int 9介於0和只需加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"); 
    } 
+0

這樣做會給我線程[main] (暫停(exception ArrayIndexOutOfBoundsException))Intergers.main(String [])行:47 \t on'occurrences [b] ++;'並做'int High = 10; \t int Low = 1; \t int random = r.nextInt(High-Low)+ Low;'給我這個http://pastebin.com/EvvpREq6 –

+0

@RyanMills這是一個單獨的問題,你處理'出現'數組的方式。請參閱編輯 – Eran

0

我有一個業餘的做法。

  1. 創建包含1-9
  2. 一個ArrayList創建一個數組來存儲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); 

    }