从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

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

法里斯
  1. 我认为您对 rand.nextInt(13) 的作用有误解。在这里,您正确地创建了随机对象,但是在第二行中,您认为将 rand 设置为 0-12,而实际上它只是调用一个方法来返回一个 0-12 之间的随机整数,并且完全不做任何事情。

    Random rand = new Random(); //random construction
    rand.nextInt(13); // Sets rand to 0-12 int
    
  2. 一种。您将变量 randomNums 设置为某个随机值,我不知道为什么,当您在任何可能的使用之前在 for 循环中将其重置为零时。

    您存储卡片的一般方法是不正确的,因为您应该制作一个新的 ArrayList 来存储 5 张卡片,但实际上,您只生成了一张卡片并打印了 5 次

    int randomNums = rand.nextInt(myCards.length);
    String cardInHand = myCards[(int)randomNums];
    for (randomNums=0; randomNums < 5; randomNums++) {
        System.out.println(cardInHand);
    }
    

这就是代码的更清晰、更合乎逻辑的版本应该是什么样子。

String[] myCards = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"};
Random rand = new Random();
Scanner scan = new Scanner(System.in);
int cardIndex = rand.nextInt(myCards.length);
ArrayList<String> playerHand = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    while (playerHand.contains(myCards[cardIndex]))
        cardIndex = rand.nextInt(myCards.length);
    playerHand.add(myCards[cardIndex]);
}
System.out.print("Guess the card in my hand: ");
if(playerHand.contains(scan.nextLine()))
    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(playerHand);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用LINQ通过一个查询从数组中获取前五个元素和后五个元素

从数组中获取随机元素,而无需在Java中重复

如何从数组中获取下一个和前五个元素?

如何从Java中的HashSet获取100个随机元素?

在Swift中从数组中获取随机元素

如何从数组中获取随机元素?

随机获取n个元素数组,并将其插入指定的元素中

从数组中随机选择n个元素

如何从Javascript中的数组中获取最大五个值?

如何从数组中获取一个随机元素而又不多次获取同一元素?

Java - 在随机数组中搜索元素

如何从数组中获取比数组更多的随机元素?

如何从二维数组中随机获取一对元素(java)

如何在Java中打印矩阵的前五个元素

如何跳过Java字符串数组中的3个元素并获取下3个元素

在Java中的布尔数组中选择一个随机元素

如何从数组中获取一些随机元素?

根据最大值从数组中获取随机元素

通过泛型函数获取数组中的随机元素

获取Ruby中数组的随机非重复元素

如何获取,返回和删除数组中的随机元素?

PHP如何从多维数组中获取随机元素?

如何从PHP中的多维数组获取随机元素

如何从每个类中随机获取一定数量的numpy数组中的至少一个元素?

Java:在3D数组中随机排列数组元素

Swift从数组中获取x个元素

如何从向量或数组中随机选择一个元素?

如何从数组中随机选择一个元素

从python中的数组中选择3个随机元素