2014-11-02 177 views
0

我有一個數組a [5] [5]和幾個值。我想根據我創建的隨機百分比隨機分配值。例如,一個值是6,數組中元素的25%應該具有值6.到目前爲止:我創建了隨機百分比,並且可以爲數組中的不同元素隨機分配值。我的問題是:超過25%的元素有6個。我的問題是:如何確保在給元素賦值6時,數組a [5] [5]中元素的25%具有該值?我是編程新手,嘗試過幾次循環和ifs,但沒有任何解決方案適合我。請把我推向正確的方向。Java二維數組隨機分配

+3

請發表你有那麼我們可以幫助在其 – chiwangc 2014-11-02 03:01:01

+1

提高** **如何準確你*創建隨機百分比*和代碼**如何** *你能隨機分配值數組中的不同元素*?幫助我們幫助你。 – 2014-11-02 05:21:07

+0

使用5x5的陣列有25個元素,你將永遠無法獲得* 25%的任何東西,因爲25%是1/4,25個元素不能被4整除。 – pjs 2014-11-02 17:55:11

回答

0

我會做這樣的

import java.util.Random; 
import java.util.Scanner; 

class Random2darray { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // Create array; you specified 5x5 but you probably shouldn't hard code like this 
     int[][] numbers = new int[5][5]; 
     //Create a scanner to get user input 
     Scanner scanner = new Scanner(System.in); 
     //I am not doing anything to make sure they give a decimal so the program won't work 
     //right if you are not giving a decimal 
     System.out.println("How much do you want to fill? Enter a decimal."); 
     //Get a double 
     double populatePercentage = scanner.nextDouble(); 
     System.out.println("What number do you want to fill with?"); 
     //Get an int 
     int fillNumber = scanner.nextInt(); 
     //Don't hardcode like this 
     int fillTimes = numberOfTimesToFill(25, populatePercentage); 
     //This is declared outside of the loops because otherwise it will be 
     //reset each time the loop runs 
     int count = 0; 
     //Create a random number generator; default seed is current time which is good enough for this 
     Random rand = new Random(); 
     //Fill the array with numbers 
     for (int i = 0; i < 5; i++) { 
      for (int j = 0; j < 5; j++) { 
       //Check if you have filled with given number enough times 
       if(count < fillTimes){ 
        numbers[i][j] = fillNumber; 
        //Increment count by 1; this is how you are keeping track 
        count++; 
       }else{ 
        //Otherwise pick a random number generator 
        //This will pick a random int between 0 - 50 inclusive 
        int randomNumber = rand.nextInt(51); 
        //This will make sure that you are not generating random numbers that are the same 
        //as the number you are filling with; This says if it is the same generate a new number 
        while(randomNumber == fillNumber){ 
         randomNumber = rand.nextInt(51); 
        } 
        //Once we know its random fill the spot in the array 
        numbers[i][j] = randomNumber; 
       } 
      } 
     } 
     //Print the array out 
     printArray(numbers); 
    } 

    private static void printArray(int[][] numbers) { 
     //Just loop through and print every number 
     for (int i = 0; i < 5; i++) { 
      for (int j = 0; j < 5; j++) { 
       //Print with formatting: this says print a digit (%d) followed by a 
       //tab(\t) and then specifies what number I want to print 
       System.out.printf("%d\t", numbers[i][j]); 
      } 
      //Print a new line after each row 
      System.out.printf("\n"); 
     } 
    } 

    private static int numberOfTimesToFill(int elements, double populatePercentage) { 
     System.out.println((int)(populatePercentage * elements)); 
     //Cast to an int to truncate the decimal part of the number because you can't have 
     //a fraction of an element 
     return (int)(populatePercentage * elements); 
    } 

} 

我評論的代碼來解釋一切呢。然而,它不能防止不良的用戶輸入,並且硬編碼數組的大小,你應該總是儘量避免硬編碼。在這個例子中,我做了(並標記在哪裏)更清晰。正如在上面的評論中提到的那樣,對於你想要的情況你完全想要25%的25%是不可能的。這是因爲.25 * 25 = 6.25,並且不能有.25的元素。

1

25的25%是大約6.這是不準確的。這是一個可能的解決方案。

import java.util.Random; 
import java.util.Scanner; 

public class Matrix25 { 
public static void main(String[] args) { 
    int[][] Matrix = new int[5][5]; 
    Scanner scanner = new Scanner(System.in); 
    System.out.print("Enter the number you want to fill the array to 25%: "); 
    int number = scanner.nextInt(); 
    int percentage=25; 
    generateMatrix(Matrix,number,percentage); 
    printMatrix(Matrix);   
} 

public static void generateMatrix(int Matrix[][],int num, int perc) {   
    int n; 
    int max=Matrix.length*Matrix[0].length; 
    int[] numbers = new int[max]; 
    Random rnd = new Random(); 
    int m = (int)(max * (perc/100.0)); 
    int x=num>m?m-1:m; 
    for(int i=0;i<max;i++) 
      numbers[i]=(i<x)?num:i+1; 

    for(int i=0;i<Matrix.length;i++) 
     for(int j=0;j<Matrix[i].length;j++) { 
      n=rnd.nextInt(max); 
      Matrix[i][j]=numbers[n]; 
      numbers[n]=numbers[max-1]; 
      max--; 
     } 
} 

public static void printMatrix(int Matrix[][]) { 
    for(int i=0;i<Matrix.length;i++) { 
     for(int j=0;j<Matrix[i].length;j++) 
       System.out.printf("%3d\t",Matrix[i][j]); 
     System.out.print("\n"); 
    } 
} 
}