我必须在数组中取整数并以随机顺序输出它们

时间:2017-10-14 19:42:25

标签: java arrays

这是我需要的输出(  输入数组: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]);
            }
          } 
        }
      }

2 个答案:

答案 0 :(得分:0)

问题是你的代码在下面的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;
    }

答案 1 :(得分:0)

感谢大家的帮助,但我没有学到其他人的任何事情  我发现了一种更简单的方法

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

}