随机随机抽取Java中的整数数组

时间:2016-11-08 19:07:50

标签: java arrays

这是我第一次使用数组。 我应该提示用户输入5个数组值,然后以随机顺序显示它们。 我很困惑,因为这是我第一次这样做。 无论如何,我的代码在这里。

import java.util.*;
public class Test {
    public static void main(String[] args) {
        int myArray[] = new int[5];
        System.out.println("Please enter 5 numbers: ");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < myArray.length - 1; i--) {
            int j = (int) (Math.random() * (i + 1));
            myArray[i] = input.nextInt();


            System.out.println("The numbers are: ");

            System.out.println(myArray[0]);
            System.out.println(myArray[1]);
            System.out.println(myArray[2]);
            System.out.println(myArray[3]);
            System.out.println(myArray[4]);


            int temp = myArray[i];
            myArray[i] = myArray[j];
            myArray[j] = temp;

            System.out.println("The numbers, shuffled, are: ");
            System.out.println(myArray[0]);
            System.out.println(myArray[1]);
            System.out.println(myArray[2]);
            System.out.println(myArray[3]);
            System.out.println(myArray[4]);
        }
    }
}

感谢大家的支持。

2 个答案:

答案 0 :(得分:0)

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Test {

    public static void shuffle(int[] arr) {
        Random rnd = ThreadLocalRandom.current();
        for (int i = arr.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            int t = arr[index];
            arr[index] = arr[i];
            arr[i] = t;
        }
    }

    public static void main(String[] args) {
        int myArray[] = new int[5];
        System.out.println("Please enter 5 numbers: ");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < myArray.length; i++) {
            System.out.println("Enter " + (i + 1) + ". number: ");
            myArray[i] = input.nextInt();
        }
        System.out.println("The numbers are: ");
        for (int j2 = 0; j2 < myArray.length; j2++) {
            System.out.println(myArray[j2]);
        }

        shuffle(myArray);

        System.out.println("The numbers, shuffled, are: ");
        for (int j2 = 0; j2 < myArray.length; j2++) {
            System.out.println(myArray[j2]);
        }
    }
}

答案 1 :(得分:0)

A - 解释

我们假设您按顺序将输入值设为{'1','2','3','4','5'}。什么改组随机破坏了顺序,所以你必须随机改变元素的位置。

在演示代码中,

  • swapArrayElement交换那些位置作为参数传递的元素。

  • getRandom返回0和range之间的随机值,该值作为参数传递给方法。

  • shuffleArray通过随机更改元素的位置来混洗数组。请通知有一个额外的boolean isShuffled[]数组,它是布尔值,因为我们必须保持位置的跟踪,无论它们是否被洗牌。

  • isArrayShuffled方法,检查是否所有位置都被洗牌。

B - 演示代码

import java.util.Scanner;

public class Test {

    public static final int ARRAY_LENGTH = 5;

    public static void main(String[] args) {
        int myArray[] = new int[ARRAY_LENGTH];



        Scanner input = new Scanner(System.in);
        System.out.println("Please enter 5 numbers: ");

        for(int i = 0; i < myArray.length; i++)
            myArray[i] = input.nextInt();

        System.out.println("\nThe numbers are: ");
        printIntArray(myArray);

        shuffleArray(myArray);
        System.out.println("\nThe numbers, shuffled, are: ");
        printIntArray(myArray);

        input.close();  // no memory leaks!
    }

    // method for printing array
    public static void printIntArray(int[] array) {
        for(int i = 0; i < array.length; i++)
            System.out.printf("%2d ", array[i]);
        System.out.printf("%n");    // use %n for os-agnostic new-line
    }

    // method for shuffling array
    public static void shuffleArray(int[] array) {
        int range = array.length;
        boolean isShuffled[] = new boolean[range];  // store which positions are shuffled

        while(!isArrayShuffled(isShuffled)) {
            int positionSrc = getRandom(range);
            int positionDst = getRandom(range);

            swapArrayElement(array, positionSrc, positionDst);
            isShuffled[positionSrc] = true;
            isShuffled[positionDst] = true;
        }

    }

    public static int getRandom(int maxRange) {
        return (int)(Math.random()*maxRange);
    }

    public static void swapArrayElement(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static boolean isArrayShuffled(boolean[] isShuffled) {
        for(int i = 0; i < isShuffled.length; i++)
            if(isShuffled[i] == false)
                return false;

        return true;
    }
}

C - 演示输出

Please enter 5 numbers: 
1 2 3 4 5

The numbers are: 
 1  2  3  4  5 

The numbers, shuffled, are: 
 4  2  5  1  3