首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从java数组中获取五个随机元素

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

Stack Overflow用户
提问于 2018-09-13 06:35:51
回答 2查看 196关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
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);
    }
}

这是输出

代码语言:javascript
复制
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

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

EN

回答 2

Stack Overflow用户

发布于 2018-09-13 07:15:16

我写了新的代码,以提高您在阅读源代码时的歧义。所以我在一些行中添加了注释,以便更清楚地理解。

代码语言:javascript
复制
public class CardGuessingGame  {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        //list of cards
        List<String> myCards = Arrays.asList("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace");
        //shuffle cards
        Collections.shuffle(myCards);

        //get first 5 elements from shuffled list
        HashSet<String> myHand = new HashSet<>();
        for (int i = 0; i < 5; i++) {
            myHand.add(myCards.get(i));
            System.out.println(myCards.get(i));
        }

        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 set contains , you found the card
        if (myHand.contains(s)) {
            System.out.println("I have that card in my hand");
        } else {
            System.out.println("I do not have that card in hand!");
        }

        System.out.print("These are the cards which is in my hand: ");
        //write all cards in my hand with sepearete comma
        System.out.println(String.join(",", myHand));
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-09-13 07:18:34

这是一个有效的代码,并且只接受唯一的卡片。首先,你应该弄清楚哪些变量是数组,哪些不是。

rand.nextInt(13);的调用不会将Random实例设置为生成1到13之间的随机数,但实际上会生成一个随机数。

你的随机数生成应该放在循环中。手头的卡片需要包含多个值,因此应该使用数组。

代码语言:javascript
复制
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

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

        String[] cardsInHand = new String[5];

        List<String> cards = new LinkedList<>(Arrays.asList(myCards));
        Collections.shuffle(cards);

        for (int i = 0; i < 5; i++) {
            //This will not give you unique cards!
            //int randomNums = rand.nextInt(myCards.length); // Randomizes "randomNums to the length of the array"
            cardsInHand[i] = cards.remove(0);
        }

        System.out.println(Arrays.toString(cardsInHand));

        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


        //Some kind of contains method. You can traverse the array use a list or do however you like
        Arrays.sort(cardsInHand);

        if (Arrays.binarySearch(cardsInHand,s) >= 0) {

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

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

        //Close the stream
        answer.close();

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

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52304357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档