我在 Java 中做了什么错误的二十一点文件计数器?

发布于 2024-11-09 16:21:36 字数 2760 浏览 0 评论 0原文

我正在做这个练习,练习是这样说的:

*给定一个包含 3 个玩家之间的 1000 手随机二十一点手牌的输入文件(此处:blackjack.txt),计算所有游戏中任何玩家遇到的二十一点数量。

二十一点定义为任何花色的 A 和任何花色的 10 点牌(J、Q、K 或 10)。

输入文件如下所示:(作为示例)

4H 5C AD JH 9C 10H

这意味着玩家 1 拥有红心 4 和梅花 5;玩家 2 拥有方块 A 和红心 J(算作二十一点);玩家 3 有一张梅花 9 和一张红心 10。

为了解决这个问题,已知有一个标准的 52 张牌,每个新游戏都会重新洗牌。*

我认为我的方法是正确的,我的代码是正确的,但我的答案失败了,任何提示,获得正确答案的建议

这是我的代码:

import java.io.*;

public class Problema16 {

    public static void main(String args[]) {
        File archivo = null;
        FileReader fr = null;
        BufferedReader br = null;
        int counter = 0;
        //int rest = 0;

        try {
            archivo = new File("C:\\Users\\\blackjack.txt");
            fr = new FileReader(archivo);
            br = new BufferedReader(fr);
            String linea;
            String[] linea2 = null;

            while ((linea = br.readLine()) != null) //System.out.println(linea);
            {
                linea2 = linea.split(" ");

                String a = (linea2[0]);
                String b = (linea2[1]);
                String c = (linea2[2]);
                String d = (linea2[3]);
                String e = (linea2[4]);
                String f = (linea2[5]);

                if ((a.startsWith("A") && (b.startsWith("J") || (b.startsWith("Q") || (b.startsWith("K") || (b.startsWith("10")))))) || ((a.startsWith("J") || (a.startsWith("Q") || (a.startsWith("K") || (a.startsWith("10"))))) && (b.startsWith("A")))) {
                    counter++;
                    //System.out.println(a + "" + b + "");
                } else if ((c.startsWith("A") && (d.startsWith("J") || (d.startsWith("Q") || (d.startsWith("K") || (d.startsWith("10")))))) || ((c.startsWith("J") || (c.startsWith("Q") || (c.startsWith("K") || (c.startsWith("10"))))) && (d.startsWith("A")))) {
                    counter++;
                    //System.out.println(c + "" + d + "");
                } else if ((e.startsWith("A") && (f.startsWith("J") || (f.startsWith("Q") || (f.startsWith("K") || (f.startsWith("10")))))) || ((e.startsWith("J") || (e.startsWith("Q") || (e.startsWith("K") || (e.startsWith("10"))))) && (f.startsWith("A")))) {
                    counter++;
                    //System.out.println(e + "" + f + "");
                } else {
                    //sobra++;
                }
            }

            System.out.println(counter);
            //System.out.println(sobra);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是在线练习 1< /a>.我的答案是119,但是是错误的。

I am doing this exercise, the exercise says this:

*Given an input file containing 1000 random blackjack hands between 3 players (here: blackjack.txt), calculate the number of blackjacks encountered for any player in all games.

A blackjack is defined as an Ace of any suit and a 10 valued card (Jack, Queen ,King or 10) of any suit.

The input file looks like this: (as an example)

4H 5C AD JH 9C 10H

This means that player one has a 4 of hearts and a 5 of clubs; player 2 has an Ace of Diamonds and a Jack of Hearts (which counts as a blackjack); player 3 has a 9 of Clubs and a 10 of Hearts.

For the purpose of this problem, it is known that there is a standard 52 card deck which is reshuffled for each new game.*

I think I am in the correct approach, and my code is correct, but my answers fails, any hint, suggestion for getting a right answer

this is my code:

import java.io.*;

public class Problema16 {

    public static void main(String args[]) {
        File archivo = null;
        FileReader fr = null;
        BufferedReader br = null;
        int counter = 0;
        //int rest = 0;

        try {
            archivo = new File("C:\\Users\\\blackjack.txt");
            fr = new FileReader(archivo);
            br = new BufferedReader(fr);
            String linea;
            String[] linea2 = null;

            while ((linea = br.readLine()) != null) //System.out.println(linea);
            {
                linea2 = linea.split(" ");

                String a = (linea2[0]);
                String b = (linea2[1]);
                String c = (linea2[2]);
                String d = (linea2[3]);
                String e = (linea2[4]);
                String f = (linea2[5]);

                if ((a.startsWith("A") && (b.startsWith("J") || (b.startsWith("Q") || (b.startsWith("K") || (b.startsWith("10")))))) || ((a.startsWith("J") || (a.startsWith("Q") || (a.startsWith("K") || (a.startsWith("10"))))) && (b.startsWith("A")))) {
                    counter++;
                    //System.out.println(a + "" + b + "");
                } else if ((c.startsWith("A") && (d.startsWith("J") || (d.startsWith("Q") || (d.startsWith("K") || (d.startsWith("10")))))) || ((c.startsWith("J") || (c.startsWith("Q") || (c.startsWith("K") || (c.startsWith("10"))))) && (d.startsWith("A")))) {
                    counter++;
                    //System.out.println(c + "" + d + "");
                } else if ((e.startsWith("A") && (f.startsWith("J") || (f.startsWith("Q") || (f.startsWith("K") || (f.startsWith("10")))))) || ((e.startsWith("J") || (e.startsWith("Q") || (e.startsWith("K") || (e.startsWith("10"))))) && (f.startsWith("A")))) {
                    counter++;
                    //System.out.println(e + "" + f + "");
                } else {
                    //sobra++;
                }
            }

            System.out.println(counter);
            //System.out.println(sobra);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

this is the exercise online 1. My answers is 119, but is wrong.

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

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

发布评论

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

评论(3

笑着哭最痛 2024-11-16 16:21:36

每个 if 中的条件很难用所有大括号阅读,但看起来还不错。
你的问题在于else if。玩家 #1 和玩家 #2(以及玩家 #3)可以同时玩二十一点,因为您的代码只会计算其中的 1 点。

删除 else 就可以了。

您还可以稍微提高代码的可读性。一些提示:

  • 检查第二/第三玩家的手牌涉及与第一名玩家相同的逻辑。使用循环。
  • 也许您可以计算手牌的价值并检查它是否等于 21,而不是显式检查特定的牌。最好将该逻辑放在单独的方法中。
  • 计算手牌价值的一种简单方法(假设输入始终合法)是放弃花色(因为它无关紧要)并使用预定义的地图来确定牌值(例如“J”=> 10,“A”- 11 等) .)

Your conditions in each if are hard to read with all the braces but seem OK.
Your problem lies in else if. Player #1 and Player #2 (and Player #3) can have blackjack at the same time where as your code will only count 1 of them.

Drop the else and you should be OK.

You can also improve the readability of your code a little. Some pointers:

  • Checking 2nd / 3rd players' hands involves the same logic as first. Use a loop.
  • Instead of explicitly checking for specific cards perhaps you can calculate the value of the hand and check if it equals 21. It'd be nice to put that logic in a separate method.
  • A simple way to calculate hand's value (assuming input is always legal) is to drop the suit (since it's irrelevant) and use a predefined map to determine card value (e.g. "J" => 10, "A" - 11, etc.)
我爱人 2024-11-16 16:21:36

用于验证的简短 sed 命令:

sed 's/[CDHS] / /g;s/[CDHS]\r$//g;s/[JKQ]/+/g;s/10/+/g;s/A/-/g;s/\(. .\)/[\1]/g;s/+ -/ * /g;s/- +/ * /g' blackjack.txt | grep " \* " | wc -l 
119 lines

删除颜色,将 JKQ10 与 + 结合,A 与 - (有点多余),+- 和 -+ 与 * (BJ) 结合。

多重匹配:

sed 's/[CDHS] / /g;s/[CDHS]\r$//g;s/[JKQ]/+/g;s/10/+/g;s/A/-/g;s/\(. .\)/[\1]/g;s/+ -/ * /g;s/- +/ * /g' blackjack.txt | grep " \*.*\* " 
[6 5] [ * ] [ * ]
[3 +] [ * ] [ * ]
[ * ] [ * ] [4 4]
[5 8] [ * ] [ * ]

4x 2hits。 119+4 = 123

当然可以使用模式匹配以类似的方式生成 java 解决方案。

A short sed-command to verify:

sed 's/[CDHS] / /g;s/[CDHS]\r$//g;s/[JKQ]/+/g;s/10/+/g;s/A/-/g;s/\(. .\)/[\1]/g;s/+ -/ * /g;s/- +/ * /g' blackjack.txt | grep " \* " | wc -l 
119 lines

delete colors, combine JKQ10 to +, A to - (a bit superflous), +- and -+ to * (BJ).

Multimatches:

sed 's/[CDHS] / /g;s/[CDHS]\r$//g;s/[JKQ]/+/g;s/10/+/g;s/A/-/g;s/\(. .\)/[\1]/g;s/+ -/ * /g;s/- +/ * /g' blackjack.txt | grep " \*.*\* " 
[6 5] [ * ] [ * ]
[3 +] [ * ] [ * ]
[ * ] [ * ] [4 4]
[5 8] [ * ] [ * ]

4x 2hits. 119+4 = 123

Patternmatching could of course be used to produce a java solution in a similar way.

青巷忧颜 2024-11-16 16:21:36

我不喜欢你的做法。它太复杂了。我将创建一个具有值字段的 Card 类(带有一个采用字符串(在文件中找到的字符串)的构造函数)。这样你就可以得到每手 2 张牌并检查值是否等于 21。

I don't like your approach. Its too complicated. I would create a Card class (with a constructor that takes a String, the ones found in the file) that has a value field. That way you could just get each 2-card hand and check if the values equals 21.

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