简体   繁体   中英

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

This is the output i need ( Input Array: 1 2 3 4 5 6 7 Random Output: 7 2 3 6 1 5 4)

this is what i get

Input size of the Array

5

Input Value

1

Input Value

2

Input Value

3

Input Value

4

Input Value

5

Random Output: 2

Random Output: 0

Random Output: 0

Random Output: 0

Random Output: 0

The problem is with line 23 and im not sure how to fix it

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

The problem is your code is going one too many indexes in the following for loop:

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

That's because you have to remember an array with say 5 elements has indexes 0-4. So while the size is the number of elements in the array the largest index is always (the number of elements) - 1.

So you should write the for loop like so:

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

Even then your code doesn't quite randomize correctly. The easiest way to randomize an array would be to swap out each element with another random element, like this:

        //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;
    }

thanks everyone for the help but i didnt learn any of the things in the others so
i found a easier way to do it

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM