Java - 我正在编写我的第一个项目。这是二十一点,但我遇到了一些麻烦

时间:2014-09-04 00:06:27

标签: java

我是编程新手。我试图制作二十一点,我遇到了一些问题。

我的控制台输出:

Would you like to play again? y/n 
y
You have: $50
Whats your bet: 
12
You get a 7 and a 10
Your total is 17
The dealer has a 9 11 showing
Would you like to hit? y/n : 
n
The dealer has 20                            //Here it is supposed to say You lost blah blah blah 
Would you like to play again? y/n         //but its skipping to System.println("play again");

package loops;
import java.util.Scanner;
import java.util.Random;
public class loops {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        char hit = 0;
        char playAgain = 0;
        String dolphin = "banana";
        int c = 5;
        int win = 0;
        int[] cards = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};
        int bet, money = 999999999, cardValue = 0, dealersValue = 0;
        if(money == 999999999){
            System.out.println("How much money would you like to start with?");
            money = reader.nextInt();
        }


        do {
            shuffleArray(cards);
            System.out.println("You have: $" + money);
            System.out.println("Whats your bet: ");
            bet = reader.nextInt();
            if(bet > money){
                System.out.println("You cannot bet over " + money + "!");
                System.exit(0);
                break;
            }


            System.out.println("You get a " + cards[0] + " and a " + cards[1]);
            cardValue = cards[0] + cards[1];
            System.out.println("Your total is " + cardValue);

            if(cardValue == 21){
                win = 1;
            }


            if(cardValue > 21){
                win = 2;
            }


            if(win != 2){

                int i = 9;
                System.out.println("The dealer has a " + cards[7] + " " + cards[8] + " showing");
                dealersValue = cards[7] + cards[8];
                System.out.println("Would you like to hit? y/n : ");
                hit = reader.next().charAt(0);
                while(dealersValue <= 17){
                    if(dealersValue <= 17){
                        dealersValue = cards[7] + cards[8] + cards[i];
                    }


                    if(dealersValue <= 17){
                        dealersValue = cards[7] + cards[8] + cards[i] + cards[i];
                    }   


                    if(dealersValue <= 17){
                        dealersValue = cards[7] + cards[8] + cards[i] + cards[i] + cards[i];
                    }

                    if(cards[i] > 11 && dealersValue > 21){
                        cards[i] = 1;
                    }
                    if(cards[7] > 11 && dealersValue > 21){
                        cards[7] = 1;
                    }
                    if(cards[8] > 11 && dealersValue > 21){
                        cards[8] = 1;
                    }

                    i++;
                }}


            while(hit == 'y'){
                System.out.println("You get a " + cards[c]);
                cardValue = cardValue + cards[c];
                System.out.println("Your total is " + cardValue);
                if(cardValue > 21){
                    if(cards[5] > 11 && cardValue > 21){
                        cards[5] = 1;
                        System.out.println("To avoid busting, you 11 changes to 1");
                    }

                    if(cards[6] > 11 && cardValue > 21){
                        cards[6] = 1;
                        System.out.println("To avoid busting, you 11 changes to 1");
                    }

                    if(cards[7] > 11 && cardValue > 21){
                        cards[7] = 1;
                        System.out.println("To avoid busting, you 11 changes to 1");
                    }

                    if(cards[8] > 11 && cardValue > 21){
                        cards[8] = 1;
                        System.out.println("To avoid busting, you 11 changes to 1");
                    }

                    if(cards[9] > 11 && cardValue > 21){
                        cards[9] = 1;
                        System.out.println("To avoid busting, you 11 changes to 1");
                    }

                    if(cardValue > 21){
                        break;
                    }
                }
                System.out.println("Would you like to hit? y/n : ");
                hit = reader.next().charAt(0); 
                c++;
                if(hit =='n'){
                    System.out.println("You stay!");
                    cardValue = cardValue + 0;
                    break;
                }
            }
            System.out.println("The dealer has " + dealersValue);

            if(dealersValue > 21){
                win = 1;
            }
            if(cardValue > 21){
                win = 2;
            }
            if(cardValue == 21){
                win = 1;
            }
            if(dealersValue == 21){
                win = 2;
            }
            if (dealersValue > cardValue && dealersValue > 21){
                win = 2;
            } 
            if (dealersValue == cardValue){
                win = 2;
            }
            if(dealersValue < cardValue && cardValue <=21) {
                win = 1;
            }
            if(dealersValue == 21 && dealersValue != cardValue || dealersValue == 21 && dealersValue == cardValue || cardValue > 21){
                win = 2;
            }
            if(cardValue == 21 && dealersValue  != cardValue || dealersValue > 21){
                win = 1;
            }
            if(win == 1){
                System.out.println("You won "+bet*1 + " dollars!");;
                System.out.println("Your total balance is " + money);
            }
            if(win == 2){
                System.out.println("You lost "+bet*1 + " dollars!");;
                System.out.println("Your total balance is " + money);
            }
            if(win == 3){
                System.out.println("You lost "+bet*1 + " dollars!");;
                System.out.println("Your total balance is " + money);
            }
            if(money <= 0){
                System.out.println("You are out of money!");
                System.exit(0);
            }
            System.out.println("Would you like to play again? y/n ");
            playAgain = reader.next().charAt(0);

            if(playAgain == 'n'){
                System.exit(0); }
            win = 0;

        } while (playAgain == 'y');
    }


    static void shuffleArray(int[] cards){
        Random rnd = new Random();
        for (int i = cards.length - 1; i > 0; i--)
        {
            int index = rnd.nextInt(i + 1);
            int a = cards[index];
            cards[index] = cards[i];
            cards[i] = a;
        }
    }
}

我已经多次梳理了代码,寻找我出错的地方,比如它可能不是},但我找不到任何东西。我觉得它可以用一些新鲜的眼睛! 在我使用此代码后,我计划再次尝试使用面向对象的方法。

1 个答案:

答案 0 :(得分:2)

设置变量&#34; win&#34;的if语句集。不正确。

通过给出的示例遍历代码(经销商有20个,玩家有17个),变量&#34; win&#34;从来没有设置所有的陈述打印出来#34;你输了#34;和#34;你赢了&#34;被跳过了。

很有可能在这一行

if (dealersValue > cardValue && dealersValue > 21){

你的意思

dealersValue < 21

然后将win设置为2并显示正确的输出。

调试提示:使用IDE浏览代码。如果你逐行一步,你应该立即看到它跳过设置&#34; win&#34;变量

祝学习编程好运!这很有趣但非常令人沮丧:)