- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的希望我能在 SO 上讽刺性地解决这样的问题。无论如何,我正在使用 java 制作一个二十一点程序,并且在尝试处理 ACE 的使用方式时遇到了错误。对于那些不了解游戏的人来说,ACE 可以算作 11 或 1,并且为了这个程序,我将其自动算作 11,除非您失败(您的分数超过 21)。我的问题是我有 3 种方法:
public int getPlayerScore() {
int pScore = 0;
for (int i = 0; i < playerHand.size(); i++) {
pScore = pScore + getCardScore(playerHand.get(i), "P");
}
return pScore;
public int getCardScore(Card c, String s) {
int cardScore = 0;
// gets the score of the card based on value
switch (c.getValue()) {
case TWO:
cardScore = 2;
break;
case THREE:
cardScore = 3;
break;
case FOUR:
cardScore = 4;
break;
case FIVE:
cardScore = 5;
break;
case SIX:
cardScore = 6;
break;
case SEVEN:
cardScore = 7;
break;
case EIGHT:
cardScore = 8;
break;
case NINE:
cardScore = 9;
break;
case TEN:
cardScore = 10;
break;
case JACK:
cardScore = 10;
break;
case QUEEN:
cardScore = 10;
break;
case KING:
cardScore = 10;
break;
case ACE:
cardScore = getAceScore(s);
break;
}
return cardScore;
}
public int getAceScore(String s) {
int aceScore = 0;
int tempScore = 0;
if(s.equals("P")){
tempScore = getPlayerScore() + 11;
}
else{
tempScore = getDealerScore() + 11;
}
// if an ace as 11 doesn't bust player
if (tempScore <= 21) {
aceScore = 11;
}
// if an ace as 11 busts player
else if (tempScore >= 21) {
aceScore = 1;
}
return aceScore;
}
我知道为什么会出现堆栈溢出错误,我的getAceScore()
正在调用getPlayerScore()
依次调用 getAceScore()
等等。我尝试拥有 int playerScore
必要时更新的全局变量,问题是它会在不应该更新时更新并更改 ACE 分数。例如,如果我的 ACE、NINE 得分为 20,那么它会说我的得分是 10,因为它会说如果 ACE 是 11,那么我的得分将为 31。我正在考虑与 getCardScore
分开制作另一种方法。 ,从该方法中删除 ACE 情况,然后不断检查玩家手中是否有 ACE,然后调用 getAceScore
。但这似乎太多了。谁能提供一个更简单的解决方案?
最佳答案
摆脱递归,你就会没事的。 getAceScore 方法可以将没有 Ace 的玩家分数作为参数。然后它会根据 11 是否会打败他而返回 1 或 11
public int getAceScore( int playerScoreWithoutAce ) {
int aceScore = ( playerScoreWithoutAce + 11 <= 21 ) ? 11 : 1;
return aceScore;
}
我想无论如何我都会以不同的方式对整个解决方案进行建模。您应该只拥有 Player 对象,并避免整个“如果庄家/如果玩家”的区别。 Player 对象在某个时刻有一个方法来计算他手牌的得分。这就是决定 Ace 值(value)的方法
public int handScore( List<Card> cards ) {
int handScore = 0;
// This assumes cards come in sorted with Ace last
for( Card card : cards ) {
int cardValue = ( card == ACE ) ? aceValue( handScore ) ? card.getValue();
handScore += cardValue;
}
return handScore;
}
private int aceValue( int handScore ) {
return ( handScore + 11 <= 21 ) ? 11 : 1;
}
关于java - 方法调用的 Stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22887153/
使用 htaccess 我能够隐藏文件扩展名,使其成为/home 而不是/home.html。但是,对于主页,我想完全隐藏文件名,例如 stackoverflow.com,而不是 stackoverf
我试图在关闭子窗体时关闭我的主(父)窗体。然而,这给了我一个 StackOverflow 异常。 但是,如果我在 FormClosed 事件上调用 _child.Dispose,它会按预期工作。我应该
我在 IDEA 中使用 Gradle 并得到以下输出。 * What went wrong: A problem occurred evaluating script. > java.lang.Sta
我是一个 java 新手,并且在 StackOverflow 错误/在类之间访问文件的能力方面遇到了一个非常令人困惑的问题。我知道根本原因可能是我进行了一些递归调用,但修复它的语法却让我无法理解。我认
我需要编写一个程序来生成一些随机数,而不让两个数字落在某个范围内。不幸的是,我需要在学校做这件事,而我在计算机上唯一可以使用的语言是 Java(否则我会用 C++ 来做)。我对 Java 知之甚少,我
尝试设置一个 Person 类 我在尝试将信息封装在类中以使其不会被意外更改时遇到了问题。除了当我尝试使用 setter/getters 进行封装时,该类工作得非常好。我认为问题是这些方法最终会互相循
使用 scala,我已将大约 100000 个节点添加到链表中。当我使用函数 length 时,例如 mylist.length。我收到“java.lang.StackOverflowError”错误
我正在开发一个学校项目,该项目必须有 3 个带 1 个指针的快速排序、3 个带 2 个指针和 2 个堆的快速排序。 到目前为止,我已经编写了快速排序和堆之一。我遇到的问题是快速排序在小情况下工作正常,
我正在制作一个程序,它生成列表的所有子集,然后排列所有这些子集。我有一个生成子集的方法和一个生成排列的方法。但是,它们不能一起工作,即当我排列子集时,我没有得到子集的所有排列,并且出现 StackOv
我有以下函数,它应该产生笛卡尔平面中的所有坐标,我可以在n步中从原点到达: 原点是“位置”,步数是“强度”,它是一个 1-10 的整数。但是,我不断收到 stackoverflow 错误。每次我调用它
当我尝试使用 scala lib(使用 re2)匹配上述正则表达式时,代码进入以下路径并超时 1 分钟: 正则表达式: (([a-z0-9!#$%&'*+?^_`{|}~-]+(?:.[a-z0-9!
我正在开发一个运输 map 项目。我为该位置创建了一个名为 Chart 的对象和一个名为 Pos 的对象。我无法开始工作的是,当我从文本文件读取图表以正确创建对象 Pos 时,因为这就是失败的地方。
public class SortAnimal { public static Comparator getAnimalTagComparator() { return
我因使用以下泛型而收到 StackOverflowError: 积分: package me.expdev.gkitpvp; /* * Project created by ExpDev */ i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我为一些命题逻辑表达式编写了一个javaCC解析器。表达式可能会很长,有 30k 个字符。 当我解析如此大的表达式时,出现堆栈溢出异常。 是否有一些虚拟机参数决定堆栈大小? 或者在这种情况下你会怎么做
这个问题已经有答案了: What is a StackOverflowError? (15 个回答) 已关闭 8 年前。 最高数字double可以代表极高,我想。尽管下面的代码会抛出异常。这实际上是我
我正在为我的 API 构建一个 SDK,其中有两个模块,分别是 A 类和 B 类。 public class A{ public final B = new B(); public f
执行此递归时,我收到堆栈溢出错误。有一个模式,它第一次说: at MazeGui.move(MazeGui.java:79) which is line if(rigting.goal == true
我编写的这段代码在达到一定的输入大小之前效果很好。如果输入太大,我会收到“java.lang.StackOverflowError”。我已经阅读了 stackoverflow 上有关此主题的其他一些条
我是一名优秀的程序员,十分优秀!