黑杰克爪哇

时间:2015-11-25 05:16:33

标签: java eclipse

我试图在Eclipse中编写一个黑杰克程序,当程序交易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 PlayBlackJack.main(PlayBlackJack.java:72)"

有人可以提供帮助吗?我有一个单独的类,如果生成的随机卡是一个ace,它返回值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();

}

}

2 个答案:

答案 0 :(得分:1)

我无法确定哪一行是您的代码的第72行,但我可以告诉您,这很可能基于您给我们的内容,不知道您的morenext变量变成空字符串(即“”)。如果您尝试拨打charAt(0)获取0长度String,则会获得StringIndexOutOfBoundsException

答案 1 :(得分:1)

我认为因为你使用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();,并检查下一个是否为空。

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