Я пытаюсь написать программу черного джек в Eclipse, и у меня возникла проблема, когда программа имеет дело с Ace. Я спросил пользователь, если они хотят, чтобы Туз стоит 1 или 11. Он делает это, но когда я ввожу значение, он выдает сообщение об ошибкеBlack Jack Java
"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)"
Может кто-нибудь помочь с этим? У меня есть отдельный класс, который, если созданная случайная карта является тузом, возвращает значение 11. Вот эта часть кода
Обновление: добавляет значение Ace к общей сумме пользователя. Но после того, как раздался Ace, и пользователь выберет значение, независимо от того, что общего, он останавливает обращение пользователей и переходит к дилеру. Как я могу это исправить? Также у меня возникают другие проблемы. После того, как пользователь говорит «нет», чтобы получить другую карточку, он идет к дилеру и отлично работает, но затем, когда спрашивает пользователя, хотят ли они снова играть, он переходит в бесконечный цикл и начинает бросать из случайных карт. Как я могу исправить эту проблему?
import java.util.Scanner;
общественного класса 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();
}
}
который является строка номер 72 – SpringLearner
Рассмотрим обеспечение [работоспособный пример] (https://stackoverflow.com/help/mcve), который демонстрирует проблему. Это не дамп кода, а пример того, что вы делаете, что подчеркивает проблему, с которой вы сталкиваетесь. Это приведет к меньшему путанице и лучшим ответам – MadProgrammer
Как объявляется 'kbd'? –