繁体   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