为什么我的程序总是处理重复的卡片? (JAVA)

发布于 2025-01-10 21:38:15 字数 2570 浏览 3 评论 0原文

因此,对于家庭作业任务,我必须编写一个程序来处理 N(命令行输入)数量的扑克牌(5 张牌)。我有一个 for 循环来检查卡片是否已被使用,但仍然打印了重复的卡片。任何帮助将不胜感激。

检查重复项的代码

do {///check for dupes
                    bUsed = false;
                    randS = (int) (Math.random() * 4);
                    randV = (int) (Math.random() * 13);
                   
                    for (int k = 0; k < 52; k++) {
                    value[randV]);
                        if ((used[k] == (suit[randS] + value[randV])) && (used[k] != null)) {

                            bUsed = true;
                        }
                    }


                } while (bUsed);///end check

完整代码

public class Deal {
public static void main(String[] args) {
    String[] suit = {"Hearts", "Diamonds", "Spades", "Clubs"};
    String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    String[] used = new String[52];
    boolean bUsed = false;
    int usedC = 0;

    int N = Integer.parseInt(args[0]);
    // int N = 4;

    if (N <= 10) {
        /////////check for max
        for (int i = 0; i < N; i++) {/////////print N amount of decks
            System.out.println("Deck " + (i + 1));
            for (int j = 0; j < 5; j++) {/////////print 5 cards

                int randV = 0;
                int randS = 0;

                do {///check for dupes
                    bUsed = false;
                    randS = (int) (Math.random() * 4);
                    randV = (int) (Math.random() * 13);
                    
                    for (int k = 0; k < 52; k++) {
                       
                        if ((used[k] == (suit[randS] + value[randV])) && (used[k] != null)) {

                            bUsed = true;
                        }
                    }


                } while (bUsed);///end check

                used[usedC] = suit[randS] + value[randV];
                System.out.println(suit[randS] + " " + value[randV]);

                usedC++;
            }////end print 5 cards

            System.out.println();
        }///end print amount of decks
    } else {
        System.out.println("Too many decks requested");
    }
}

}

输出示例

java Deal.java 5 甲板 1 梅花 6 梅花 10 梅花 J 红心 Q 黑桃 J

牌组 2 红心 4 红心 7 红心 A 梅花 J 钻石 3

甲板 3 黑桃 K 方块 3 方块 Q 方块 4 梅花 2

牌组 4 黑桃 7 方块 7 方块 3 梅花 A 方块 9

牌组 5 梅花 2 黑桃 4 方块 4 方块 3 黑桃 7

so for a homework task I have to make a program that deals N(command line input) amount of poker decks (5 cards). I have a for-loop that checks if a card has been used, but nonetheless duplicate cards get printed. Any help would be greatly appreciated.

Code that checks for duplicates

do {///check for dupes
                    bUsed = false;
                    randS = (int) (Math.random() * 4);
                    randV = (int) (Math.random() * 13);
                   
                    for (int k = 0; k < 52; k++) {
                    value[randV]);
                        if ((used[k] == (suit[randS] + value[randV])) && (used[k] != null)) {

                            bUsed = true;
                        }
                    }


                } while (bUsed);///end check

Full code

public class Deal {
public static void main(String[] args) {
    String[] suit = {"Hearts", "Diamonds", "Spades", "Clubs"};
    String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    String[] used = new String[52];
    boolean bUsed = false;
    int usedC = 0;

    int N = Integer.parseInt(args[0]);
    // int N = 4;

    if (N <= 10) {
        /////////check for max
        for (int i = 0; i < N; i++) {/////////print N amount of decks
            System.out.println("Deck " + (i + 1));
            for (int j = 0; j < 5; j++) {/////////print 5 cards

                int randV = 0;
                int randS = 0;

                do {///check for dupes
                    bUsed = false;
                    randS = (int) (Math.random() * 4);
                    randV = (int) (Math.random() * 13);
                    
                    for (int k = 0; k < 52; k++) {
                       
                        if ((used[k] == (suit[randS] + value[randV])) && (used[k] != null)) {

                            bUsed = true;
                        }
                    }


                } while (bUsed);///end check

                used[usedC] = suit[randS] + value[randV];
                System.out.println(suit[randS] + " " + value[randV]);

                usedC++;
            }////end print 5 cards

            System.out.println();
        }///end print amount of decks
    } else {
        System.out.println("Too many decks requested");
    }
}

}

Example of output

java Deal.java 5
Deck 1 Clubs 6 Clubs 10 Clubs J Hearts Q Spades J

Deck 2 Hearts 4 Hearts 7 Hearts A Clubs J Diamonds 3

Deck 3 Spades K Diamonds 3 Diamonds Q Diamonds 4 Clubs 2

Deck 4 Spades 7 Diamonds 7 Diamonds 3 Clubs A Diamonds 9

Deck 5 Clubs 2 Spades 4 Diamonds 4 Diamonds 3 Spades 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2025-01-17 21:38:15

您正在使用 == 而不是 .equals() 来比较字符串。

这是有问题的行:

if ((used[k] == (suit[randS] + value[randV])) && (used[k] != null))

首先,您需要翻转空检查和值检查,以防止 NullPointerException,因为 .equals() 是一种方法调用(对于运算符 == 这并不重要)。然后使用该方法进行比较,例如:

if ((used[k] != null) && used[k].equals(suit[randS] + value[randV]))

除此之外,如果您使用 List 代替 used 而不是数组,您的生活会变得更加轻松,因为然后你可以简单地使用 .contains 方法而不是你自己的循环 - 但我不知道你的作业是否允许这样做。

为了使程序更快一点,您可以在 if 块内的末尾添加一个 break; 语句,因为一旦发现重复项,您就可以中断 for 循环。

You are comparing Strings with == instead of .equals().

This is the problematic line:

if ((used[k] == (suit[randS] + value[randV])) && (used[k] != null))

First of all you need to flip the null-check and the value-check, to prevent a NullPointerException, since .equals() is a method call (for the operator == this would not matter). Then use the method for comparison, like:

if ((used[k] != null) && used[k].equals(suit[randS] + value[randV]))

Besides that you would make your life a lot easier, if you used a List<String> for used instead of an array, because there you could then simply use the .contains method instead of your own loop - but I don't know if that is allowed for your homework.

To make your program a little bit faster, you can add a break; statement at the end inside the if-block, as you can break out the for-loop as soon as you find a duplicate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文