gpt4 book ai didi

java - java 黑 jack

转载 作者:行者123 更新时间:2023-12-02 04:09:44 26 4
gpt4 key购买 nike

我正在尝试在 Eclipse 中编写一个黑 jack 程序,但当该程序处理 Ace 时遇到问题。我询问用户是否希望 A 值 1 或 11。确实如此,但当我输入值时,它会给出错误消息

"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 的值添加到用户的总值中。但在发完 A 并用户选择一个值后,无论总数是多少,它都会停止用户的回合并转到庄家。我该如何纠正这个问题?我遇到的另一个问题是,在用户对想要另一张卡说“不”之后,它会转到经销商并且工作正常,但是当询问用户是否想再次玩时,它会进入无限循环并开始抛出出随机卡。我该如何纠正这个问题?

    import java.util.Scanner;

公共(public)类 PlayBlackJack {

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();

}

}

最佳答案

我认为因为您使用 kbd.nextInt() 来获取 Ace 值,所以缓冲区中会留下一个新行字符,因此当循环进行时 kbd.nextLine () 返回新行字符,而不是可能导致 more.charAt(0) 出现问题的 Y,您可能需要添加额外的 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());

关于java - java 黑 jack ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com