猿问

从java数组中获取五个随机元素

我已经在这个项目上工作了几天,我能够完成大部分工作,但我一直在努力从我的阵列中取出五个不同的项目。不过,我可以选择相同的项目五次。


这是我的代码的样子:


public class CardGuessingGame {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {


        String[] myCards = new String[]{"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"}; // Array for cards


        Random rand = new Random(); //random construction

        rand.nextInt(13); // Sets rand to 0-12 int

        //int randomNums = rand.nextInt(13); // sets randNums from 0-12

        int randomNums = rand.nextInt(myCards.length); //Randomizes "randomNums to the length of the array"

        String cardInHand = myCards[(int)randomNums];


        for (randomNums=0; randomNums < 5; randomNums++) {


            System.out.println(cardInHand);

        }


        System.out.print("Guess the card in my hand: "); // Prints to user asking for guess


        Scanner answer = new Scanner(System.in); // gets user input

        String s = answer.nextLine(); //stores user input inside variable s


        if(s.equals(cardInHand)) {


            System.out.println("I do have that card in hand");

        } else {


            System.out.println("I do not have that card in hand!");

        }


        System.out.print("These were the cards I had in hand: ");

        System.out.println(cardInHand);

    }

}

这是输出


run:

four

four

four

four

four

Guess the card in my hand: four

I do have that card in hand

These were the cards I had in hand: four

我现在所拥有的有效,但不正确。


慕田峪4524236
浏览 683回答 3
3回答

慕尼黑5688855

这是一个有效的代码,只需要独特的卡片。首先,您应该知道哪些变量是数组,哪些不是。调用rand.nextInt(13);不会将 Random 实例设置为生成 1 到 13 之间的随机数,但实际上会生成一个。你的随机数生成应该放在循环中。手中的卡片需要包含多个值,因此应使用数组。import java.util.Arrays;import java.util.Collections;import java.util.LinkedList;import java.util.List;import java.util.Scanner;public class CardGuessingGame {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param args&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the command line arguments&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String[] myCards = new String[] { "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "jack", "queen", "king", "ace" }; // Array for cards&nbsp; &nbsp; &nbsp; &nbsp; //Random rand = new Random(); // random construction&nbsp; &nbsp; &nbsp; &nbsp; //rand.nextInt(13); // Sets rand to 0-12 int&nbsp; &nbsp; &nbsp; &nbsp; // int randomNums = rand.nextInt(13); // sets randNums from 0-12&nbsp; &nbsp; &nbsp; &nbsp; String[] cardsInHand = new String[5];&nbsp; &nbsp; &nbsp; &nbsp; List<String> cards = new LinkedList<>(Arrays.asList(myCards));&nbsp; &nbsp; &nbsp; &nbsp; Collections.shuffle(cards);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //This will not give you unique cards!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //int randomNums = rand.nextInt(myCards.length); // Randomizes "randomNums to the length of the array"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cardsInHand[i] = cards.remove(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(cardsInHand));&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Guess the card in my hand: "); // Prints to user asking for guess&nbsp; &nbsp; &nbsp; &nbsp; Scanner answer = new Scanner(System.in); // gets user input&nbsp; &nbsp; &nbsp; &nbsp; String s = answer.nextLine(); // stores user input inside variable s&nbsp; &nbsp; &nbsp; &nbsp; //Some kind of contains method. You can traverse the array use a list or do however you like&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(cardsInHand);&nbsp; &nbsp; &nbsp; &nbsp; if (Arrays.binarySearch(cardsInHand,s) >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("I do have that card in hand");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("I do not have that card in hand!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //Close the stream&nbsp; &nbsp; &nbsp; &nbsp; answer.close();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("These were the cards I had in hand: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(cardsInHand));&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答