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

9nvpjoqh  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(312)

我得为我要上的课写个代码。这是一个基于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");

    }   
}
zxlwwiss

zxlwwiss1#

所以只要看看代码我就会:
声明 int userChoice = 0; 在外面做一会儿。这是while条件工作所必需的。 welcome(); 以及 userChoice = getMenuChoice(userInput); 应该移动到do while,这样它将重复欢迎消息并请求用户选择do while执行的所有操作
像这样简化while循环 while(userChoice != 3 && chipsNow > 0) . 这只是为了让它更具可读性。
最后移除 break; 他在你的房间里 if(getNum == spinNum) { } else { } . 一 break 无论是否满足while条件,都将强制退出while循环。所以基本上如果你输赢了一场比赛,你的循环就会退出,我不认为这是你想要的。如果 chipsNow < 0 或者 userChoice == 3

相关问题