无法让我的代码重复问题让游戏继续

Can't get my code to repeat the questions for the game to continue

我必须为我正在服用的 class 编写代码。这是一款基于投注 2 种颜色和 1 - 36 数字的游戏。用户已经给了他们一定数量的筹码,即 100。我已经编写了大部分代码,但是,我无法获得我的重复该过程的代码。我目前正在使用 Do-While 循环。但这就是行不通。

这是我的代码:

import java.util.Scanner;
import java.lang.Math;

public class Program_8 {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        int chipsNow = 100;
        int userChoice;
        int chipsBetted = 0;
        
        
        //welcome message
        welcome();  
        
        do {
            
            
            int spinNum = (int)(Math.random() * 36) + 0;
            userChoice = getMenuChoice(userInput);
            
            //get user choice
            if (userChoice == 1) {
                int getNum = getNumber(userInput);
                int getBet = getBet(userInput, chipsNow);
                String determineColor = determineColor(spinNum);
                
                System.out.println("\nSpinning Wheel....");
                System.out.println("Spin Number: " + spinNum);
                System.out.println("Spin Color: " + determineColor);
                
                if (getNum == spinNum) {
                    
                    chipsNow += getBet;
                    chipsBetted += getBet;
                    System.out.println("Congrats, you won!");
                    System.out.println("\nYou now have: " + chipsNow + " chips");
                    ;
                }
                
                else {
                    chipsNow -= getBet;
                    
                    System.out.println("\nSorry, you lost!");
                    System.out.println("You now have: " + chipsNow + " chips");
                    
                }
            }
            
            if (userChoice == 2) {
                String getColor = getColor(userInput);
                int getBet = getBet(userInput, chipsNow);
                String determineColor = determineColor(spinNum);
                
                System.out.println("Spinning Wheel....");
                System.out.println("Spin Number: " + spinNum);
                System.out.println("Spin Color: " + determineColor);
                
                if (getColor.equals(determineColor)) {
                    
                    chipsNow += getBet;
                    chipsBetted += getBet;
                    System.out.println("\nCongrats, you won!");
                    System.out.println("You now have: " + chipsNow + " chips");
                    
                }
                
                else {
                    
                    chipsNow -= getBet;
                    System.out.println("\nSorry, you lost!");
                    System.out.println("You now have: " + chipsNow + " chips");
                    
                    }
                }
                
        }while (userChoice != 3 && chipsNow > 0);
        
        
        
    }
    
    //welcome message
    public static void welcome() {
        
        int chipsNow = 100;
        
        System.out.println("############################");
        System.out.println("#    Welcome To Roulette   #");
        System.out.println("############################");
        System.out.println("# Number Bets Payout: 35:1 #");
        System.out.println("# Color Bets Payout: 1:1   #");
        System.out.println("############################\n");
        System.out.println("Chips owned: " + chipsNow + "\n");
        
        System.out.println("1. Pick a number to bet on");
        System.out.println("2. Pick a color to bet on");
        System.out.println("3. Cash Out\n");
        
    }
    //get menu choice
    public static int getMenuChoice(Scanner userInput) {
        int getMenuChoice;
        
        System.out.println("\nChoose an option [1-3]: ");
        getMenuChoice = userInput.nextInt();
        
        
        return getMenuChoice;
    }
    
    public static int getNumber(Scanner userInput) {
        int getNumber;
        
        do {
            
        System.out.println("Enter a number to bet on [0-36]: ");
        getNumber = userInput.nextInt();
        
        
        }while (getNumber < 0 || getNumber > 36);   
        
        
        return getNumber;
    }
    
    public static String getColor(Scanner userInput) {
        String getColor = "";
        
        do{
        System.out.println("Enter a color to bet on [Red or Black]: ");
        getColor = userInput.next();
        
        }while (!(getColor.equals("Red") || getColor.equals("Black")));
                
        return getColor;
    }
    
    public static int getBet(Scanner userInput, int chipsNow) {
        int getBet;
        
        do{
        System.out.println("Enter the number of chips to bet [1 - " + chipsNow + "]: ");
        getBet = userInput.nextInt();
        
        }while (getBet < 1 || getBet > chipsNow);
        
        
        return getBet;
    }
    
    public static String determineColor(int spinNum) {
        
        if (spinNum % 2 == 0) {
            if (spinNum == 0) {
                return "Green";
            }
            //even
            else {
                return "Red";
            }
        }
        
        return "Black";
        
        
    }
    
    public static void report(int chipsNow) {
        
        System.out.println("\nThanks for Playing!");
        System.out.println("You Won a total of: " + chipsNow + " chips today");
        
        
        
    }   
}

所以只看代码我会:

  1. 在 do while 之外声明 int userChoice = 0;。这是 while 条件起作用所必需的。

  2. welcome();userChoice = getMenuChoice(userInput); 应该进入 do while 因此它将重复欢迎消息并要求用户选择在执行时执行的所有操作

  3. 像这样简化你的 while 循环 while(userChoice != 3 && chipsNow > 0)。这只是为了使其更具可读性。

  4. 最后删除 if(getNum == spinNum) { } else { } 中的 break;break 将强制退出 while 循环,而不管 while 条件是否满足。所以基本上,如果你赢了或输了一场比赛,你的循环就会退出,我认为这不是你想要的。您只希望在 chipsNow < 0userChoice == 3

    时退出循环