Java 黑杰克爪哇
我正试图在Eclipse中编写一个black jack程序,当程序处理Ace时,我遇到了一个问题。我问用户,他们希望Ace值1还是11。它会这样做,但当我输入一个值时,它会给出一条错误消息Java 黑杰克爪哇,java,eclipse,Java,Eclipse,我正试图在Eclipse中编写一个black jack程序,当程序处理Ace时,我遇到了一个问题。我问用户,他们希望Ace值1还是11。它会这样做,但当我输入一个值时,它会给出一条错误消息 "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at P
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at PlayBlackJack.main(PlayBlackJack.java:72)"
有人能帮忙吗?我有一个单独的类,如果生成的随机卡是ace,它将返回值11。这是代码的一部分
更新:它将Ace的值添加到用户的总数中。但是在发了一张王牌并且用户选择了一个值之后,不管总数是多少,它都会停止用户转向并转到经销商处。我怎样才能纠正这个问题?另外一个问题是,当用户对想要另一张牌说“不”后,它会转到经销商处,效果很好,但当询问用户是否想再次玩时,它会进入无限循环并开始抛出随机牌。我怎样才能纠正这个问题呢
import java.util.Scanner;
公营21点{
public final static int MAXCARDS=52;
//declaring the constant maxcards to be 52
//since there are 52 cards in the deck
public static void main(String[] args) {
Scanner kbd=new Scanner (System.in);
String printRules;
//check to see if the user wants to see the rules or not
String more;
//variable used to see if the user would like to play the game
String next;
//variable used to see if the user would like another card
int dealerTotal, userTotal;
//keeps track of the user's total and the dealer's total
int wins=0, losses=0;
//variables used to keep track of the user's wins and losses
int card = 0;
System.out.println(" Welcome to Black Jack!");
System.out.println("Would you like to see the rules? Type yes or no");
//If yes, rules printed, if no, rules not printed
printRules=kbd.nextLine();
printRules=printRules.toUpperCase();
if (printRules.charAt(0)=='Y')
{
(print rules)
System.out.println("Now lets play!\n\n\n");
}
System.out.println("Would you like to play a game of Black Jack?");
more=kbd.nextLine();
more=more.toUpperCase();
next="Yes";
while (!more.isEmpty() && more.charAt(0)=='Y')
{
System.out.println("The game begins with this your first card:");
userTotal=0;
dealerTotal=0;
while (!next.isEmpty() && next.charAt(0)=='Y')
{
card=PickACard.findCardValue();
if (card==11)
{
System.out.println("Would you like the Ace to be a 1 or 11?");
int aceValue=kbd.nextInt();
while (aceValue!=1 && aceValue!=11)
{
System.out.println("You did not enter a 1 or 11");
aceValue=kbd.nextInt();
}
card=aceValue;
}
userTotal=userTotal+card;
System.out.println("You're total is " +userTotal);
if (userTotal>21)
{
System.out.println("Sorry, You lose");
losses++;
System.out.println("Would you like to play again?");
next="No";
more=kbd.nextLine();
more=more.toUpperCase();
}
else
{
System.out.println("Would you like another card?");
next=kbd.nextLine();
next=next.toUpperCase();
}
}
while (dealerTotal<=userTotal && userTotal<21)
{
System.out.println("Now it's the dealer's turn");
int card1=0;
card1=PickACard.findCardValue();
if (card1==11)
{
int aceValue1;
if (dealerTotal+11>21)
{
aceValue1=1;
}
else
{
aceValue1=11;
}
card1=aceValue1;
}
dealerTotal=dealerTotal+card1;
System.out.println("The dealer's total is "+dealerTotal);
if (dealerTotal==userTotal && userTotal<21)
{
losses++;
System.out.println("Sorry, you lose. Would you like to play again?");
more=kbd.nextLine();
more=more.toUpperCase();
}
if (dealerTotal>21)
{
wins++;
System.out.println("You Win! Would you like to play again?");
more=kbd.nextLine();
more=more.toUpperCase();
}
/*else
{
losses++;
System.out.println("You lose. Would you like to play again?");
more=kbd.nextLine();
more=more.toUpperCase();
}*/
}
}
System.out.println("You won "+wins+" game(s) and lost "+losses+" game(s)");
kbd.close();
public final static int MAXCARDS=52;
//将常量MAXCARD声明为52
//因为牌组中有52张牌
公共静态void main(字符串[]args){
扫描仪kbd=新扫描仪(System.in);
字符串打印规则;
//检查用户是否希望查看规则
多串;
//变量,用于查看用户是否愿意玩游戏
下一个字符串;
//变量,用于查看用户是否需要另一张卡
int dealerTotal,userTotal;
//跟踪用户总数和经销商总数
积分赢=0,损失=0;
//用于跟踪用户胜负的变量
int卡=0;
System.out.println(“欢迎来到Black Jack!”);
System.out.println(“您想查看规则吗?键入是或否”);
//如果是,则打印规则;如果否,则不打印规则
printRules=kbd.nextLine();
printRules=printRules.toUpperCase();
if(printRules.charAt(0)='Y')
{
(打印规则)
System.out.println(“现在播放!\n\n\n”);
}
System.out.println(“你想玩黑杰克游戏吗?”);
more=kbd.nextLine();
more=more.toUpperCase();
next=“是”;
而(!more.isEmpty()&&more.charAt(0)='Y')
{
System.out.println(“游戏以您的第一张牌开始:”);
userTotal=0;
dealerTotal=0;
而(!next.isEmpty()&&next.charAt(0)='Y')
{
card=PickACard.findCardValue();
如果(卡==11)
{
System.out.println(“您希望Ace是1还是11?”);
int aceValue=kbd.nextInt();
while(aceValue!=1&&aceValue!=11)
{
System.out.println(“您没有输入1或11”);
aceValue=kbd.nextInt();
}
卡=价值;
}
用户总数=用户总数+卡片;
System.out.println(“You's total is”+userTotal);
如果(userTotal>21)
{
System.out.println(“对不起,您输了”);
损失++;
System.out.println(“您想再次播放吗?”);
next=“否”;
more=kbd.nextLine();
more=more.toUpperCase();
}
其他的
{
System.out.println(“您想要另一张卡吗?”);
next=kbd.nextLine();
next=next.toUpperCase();
}
}
while(dealerTotal我不知道哪一行是代码的第72行,但我可以告诉你,根据你给我们的信息,很可能你的more
或next
变量变成了空字符串(即“”)。如果你尝试调用charAt(0)
对于长度为0的字符串
,您将获得StringIndexOutOfBoundsException
我认为,因为您使用kbd.nextInt()
来获取Ace值,所以在缓冲区中会留下一个新行字符,因此当循环运行时kbd.nextLine()
返回新行字符,而不是Y,这可能会导致more.charAt(0)
出现问题。您可能必须添加额外的kbd.nextLine();
以删除新行字符。正如Elliot Frisch指出的,您应该检查while控制语句中的字符串是否为空
while(!more.isEmpty() && more.charAt(0) == 'y')
{
}
我猜next.charAt(0)
正在显示错误。您可以在询问是否需要另一个游戏之前尝试执行kbd.nextLine();
,并检查next是否为空
while(!next.isEmpty() && more.charAt(0) == 'y')
{
}
试试这个
System.out.println("Sorry, You lose");
losses++;
System.out.println("Would you like to play again?");
next="No";
kbd.nextLine(); // to flush out new line character
more=kbd.nextLine();
more=more.toUpperCase();
您还可以使用nextLine()并将其解析为int,这将避免新行字符问题
aceValue = Integer.parseInt(kbd.nextLine());
哪一行号是72考虑提供一个演示您的问题的代码。这不是代码转储,而是您正在做的一个示例,它突出了您的问题。这将减少混淆并获得更好的响应如何声明kbd
?在kbd.nextL之前使用kbd
的上一次调用是什么您是否考虑过检查String.isEmpty()
?我会更新答案和kbd.nextLine();如果有帮助,请告诉我。这是我在程序运行时得到的结果,它处理一张a。像63和72是两个while循环语句:你想要另一张牌吗?是的,这张牌是红桃a你想要a是1还是11?11你总共是29对不起,你输了,你想再玩一次吗?线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:PlayBlackJack.main(PlayBlackJack.java:63)的java.lang.String.charAt(未知源代码)处的0
我怀疑@Yan的答案有问题。看起来您可能正在使用kbd
中的可读字符,但不是consumin