在它应该之前进行循环中断

时间:2017-06-05 06:52:46

标签: java

我正在学习编写java代码,目前我正在尝试制作一个“21”的基本游戏。

代码中有两个“do while”循环:

  1. 我希望玩家选择是否要画一张新卡和
  2. 我希望玩家选择是否要在最后一个游戏结束时玩新游戏。
  3. 但是,如果我用“是”或“否”回答这两个问题并不重要,循环总会中断并“结束”。

    我的逻辑中有什么问题吗?

    非常感谢你的帮助。

    import java.util.Scanner;
    
    public class twentyOneGame {
    
    
    public static void main(String[] args) {
        int computerCard;
        int playerCard;
        int playerSumCards = 0;
        int computerSumCards = 0;
        int cardAmount = 0;
        String keepPlaying = "Yes";
        String keepGoing;
    
    
        System.out.printf("Enter your name %n");
        Scanner input = new Scanner(System.in);
        String playerName = input.nextLine();
    
        do {
            do {
    
                playerDraw playerdraw = new playerDraw();
                playerCard = playerdraw.getPlayerCard();
                playerSumCards += playerCard;
    
                computerDraw computerdraw = new computerDraw();
                computerCard = computerdraw.getComputerCard();
                computerSumCards += computerCard;
                cardAmount += 1;
    
                System.out.printf("Your have %s card(s). Your total is %s. %n", cardAmount, playerSumCards);
    
                input = new Scanner(System.in);
                System.out.printf("Do you want to draw another card? Yes or No %n");
                keepGoing = input.nextLine();
    
            } while (keepGoing == "Yes" && cardAmount < 3);
    
    
            if (cardAmount == 1) {
                System.out.printf("Your total is %s %n", playerSumCards);
                System.out.printf("The computers total is %s %n", computerSumCards);
    
                if (playerSumCards > computerSumCards) {
                    System.out.printf("Good job %s, you won! %n", playerName);
                } else {
                    if (playerSumCards == computerSumCards) {
                        System.out.printf("It's a draw! %n");
                    } else {
                        System.out.printf("Oh no, the computer won! %n");
                    }
                }
            } else {
                if (cardAmount == 2) {
    
                    System.out.printf("Your total is %s %n", playerSumCards);
                    System.out.printf("The computers total is %s %n", computerSumCards);
    
                    if (playerSumCards > computerSumCards) {
                        System.out.printf("Good job %s, you won! %n", playerName);
                    } else {
                        if (playerSumCards == computerSumCards) {
                            System.out.printf("It's a draw! %n");
                        } else {
                            System.out.printf("Oh no, the computer won! %n");
                        }
                    }
                } else {
                    if (cardAmount == 3) {
    
                        System.out.printf("Your total is %s %n", playerSumCards);
                        System.out.printf("The computers total is %s %n", computerSumCards);
    
                        if (playerSumCards > computerSumCards) {
                            System.out.printf("Good job %s, you won! %n", playerName);
                        } else {
                            if (playerSumCards == computerSumCards) {
                                System.out.printf("It's a draw! %n");
                            } else {
                                System.out.printf("Oh no, the computer won! %n");
                            }
                        }
                    } else {
    
                    }
                }
            }
            input = new Scanner(System.in);
            System.out.printf("Do you want to keep playing? Yes or No %n");
            keepPlaying = input.nextLine();
    
            if (keepPlaying == "Yes") {
                playerSumCards = 0;
                computerSumCards = 0;
            } else {
                System.out.printf("Thanks for playing, %s %n", playerName);
                break;
            }
    
        } while (keepPlaying =="Yes");
    }
    }
    

0 个答案:

没有答案