我在 Java 中做了什么错误的二十一点文件计数器?
我正在做这个练习,练习是这样说的:
*给定一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个
if
中的条件很难用所有大括号阅读,但看起来还不错。你的问题在于
else if
。玩家 #1 和玩家 #2(以及玩家 #3)可以同时玩二十一点,因为您的代码只会计算其中的 1 点。删除
else
就可以了。您还可以稍微提高代码的可读性。一些提示:
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:
用于验证的简短 sed 命令:
删除颜色,将 JKQ10 与 + 结合,A 与 - (有点多余),+- 和 -+ 与 * (BJ) 结合。
多重匹配:
4x 2hits。 119+4 = 123
当然可以使用模式匹配以类似的方式生成 java 解决方案。
A short sed-command to verify:
delete colors, combine JKQ10 to +, A to - (a bit superflous), +- and -+ to * (BJ).
Multimatches:
4x 2hits. 119+4 = 123
Patternmatching could of course be used to produce a java solution in a similar way.
我不喜欢你的做法。它太复杂了。我将创建一个具有值字段的 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.