简体   繁体   English

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

[英]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)这是我需要的输出(输入数组:1 2 3 4 5 6 7 随机输出:7 2 3 6 1 5 4)

this is what i get这就是我得到的

Input size of the Array数组的输入大小

5 5

Input Value输入值

1 1

Input Value输入值

2 2

Input Value输入值

3 3

Input Value输入值

4 4

Input Value输入值

5 5

Random Output: 2随机输出:2

Random Output: 0随机输出:0

Random Output: 0随机输出:0

Random Output: 0随机输出:0

Random Output: 0随机输出:0

The problem is with line 23 and im not sure how to fix it问题出在第 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]);
            }
          } 
        }
      }

The problem is your code is going one too many indexes in the following for loop:问题是您的代码在以下 for 循环中使用了太多索引:

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

That's because you have to remember an array with say 5 elements has indexes 0-4.那是因为您必须记住一个包含 5 个元素的数组,其索引为 0-4。 So while the size is the number of elements in the array the largest index is always (the number of elements) - 1.因此,虽然大小是数组中元素的数量,但最大的索引始终是(元素数量)- 1。

So you should write the for loop like so:所以你应该像这样编写 for 循环:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个由5个整数组成的随机数组,但不将0视为整数? - How do I write a random array of 5 integers but have it not count 0 as an integer? 生成具有范围的随机整数并将它们放入此数组中 - Generate random integers with a range and place them into this array 想要按升序创建随机整数数组 - Want to create array of random integers in ascending order [Processing 2.0]尝试创建随机整数数组,然后对它们进行冒泡排序 - [Processing 2.0]Trying to create an array of random integers and then bubble sort them 我将如何产生 2 个随机整数,然后比较它们? - how would I produce 2 random integers, and then compare them? 将随机整数迭代到数组中 - Iterate random integers into an Array 将随机整数存储在数组中 - Store random integers in array 尝试以随机顺序输出列表数组的文本,但我不断收到textView.setText(myList [rando])错误。 - Trying to output text of a list array in a random order but i keep getting an error with textView.setText(myList[rando]); 生成10个随机整数,将它们存储在Array中,然后调用Method以显示Array - Generate 10 Random Integers storing them in an Array and then calling a Method to display the Array 如何从随机字符数组中提取所有整数并将它们传输到数组 - how to extract all integers from an array of random characters and transfer them to the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM