簡體   English   中英

我必須在數組中取整數並以隨機順序輸出它們

[英]I have to take integers in an array and output them in random order

這是我需要的輸出(輸入數組:1 2 3 4 5 6 7 隨機輸出:7 2 3 6 1 5 4)

這就是我得到的

數組的輸入大小

5

輸入值

1

輸入值

2

輸入值

3

輸入值

4

輸入值

5

隨機輸出:2

隨機輸出:0

隨機輸出:0

隨機輸出:0

隨機輸出:0

問題出在第 23 行,我不知道如何解決它

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

    public class problem_2 {

       public static void main(String args[]){
       Random r = new Random();
       Scanner m = new Scanner(System.in);      
       System.out.println("Input size of the Array");   
       int size = m.nextInt();
       int a[] = new int[size];
       int b[] = new int[size];

        for(int i = 0;i<a.length;i++) {         
           System.out.println("Input Value " +(i+1));
           a[i] = m.nextInt();
          }
       int cell = 0;
       int x = r.nextInt(size);
       int value = a[x];
       while(cell<size) {

        for(int i =0; i<= size;i++) {
            if (b[i]==value) {
                cell++;
            }

            if(cell==0) {
                b[cell] = value;
                cell++;
            }
            System.out.println("Random Output: "+b[i]);
            }
          } 
        }
      }

問題是您的代碼在以下 for 循環中使用了太多索引:

for(int i =0; i<= size;i++)

那是因為您必須記住一個包含 5 個元素的數組,其索引為 0-4。 因此,雖然大小是數組中元素的數量,但最大的索引始終是(元素數量)- 1。

所以你應該像這樣編寫 for 循環:

for(int i = 0; i < size;i++)

即使這樣,您的代碼也不會完全正確隨機化。 隨機化數組的最簡單方法是用另一個隨機元素交換每個元素,如下所示:

        //Randomize the array
    for(int i = 0; i < size;i++) {
            //lets get a random index in the array
            int randIndex = (int)(Math.random()*size);
            //then we will store the index we are swapping because its going to be overwritten
            int temp = a[i];
            //set our index equal to the random one
            a[i] = a[randIndex];
            //put our index's original value into the random index, so its not lost
            a[randIndex] = temp;
    }

感謝大家的幫助,但我沒有學到其他人的任何東西,所以
我找到了一種更簡單的方法

import java.util.Random;
import java.util.Scanner;
public class random{
public static void main(String args[]){
    Random r = new Random();
    Scanner m = new Scanner(System.in);     
    System.out.println("Input size of the Array");  
    int size = m.nextInt();
    int a[] = new int[size];
    int b[] = new int[size];

    for(int i = 0;i<a.length;i++) {         
        System.out.println("Input Value ");
        a[i] = m.nextInt();
    }
    int cell = 0;
    while(cell<size) {
        int n = r.nextInt(size);
        int value = a[n];
        int count = 0;
        for(int i =0; i< size;i++) {
            if (b[i]== value) {
                count++;
            }
        }
        if(count==0) {
            b[cell] = value;
            cell++;
        }

    } 
    System.out.println ("\n");
    System.out.println("Input Array: ");
    for (int i = 0; i<size; i++){      
        System.out.print(a[i] + " ");
    }    
    System.out.println ("\n");
    System.out.println("Random Output: ");
    for (int i = 0; i<size; i++){      
        System.out.print(b[i] + " ");
    }                                       
}

}

暫無
暫無

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

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