5
\$\begingroup\$

I was looking at since it was my first multi-class program, is there anything I could have done better, via making it shorter and other things as it's very long, and I'm not sure it's efficient enough or following 'etiquette' for coding with Java. Is there any advice in general that could improve this code? Thanks!

Main

import java.util.Scanner;
import java.util.Random;

public class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to the Java Roulette Game...");
    System.out.println("Have you played before? Y/N.");
    String vCheck1 = input.nextLine();

    //Validation Check -- Removes invalid inputs.
    while (!(vCheck1.equals("y")) && !(vCheck1.equals("n"))) {
        System.out.println("Incorrect, please type Y/N.");
        vCheck1 = input.nextLine();
    }

    //If user inputs 'y' (yes), print below.
    if (vCheck1.equals("y")) {
        System.out.println("Good luck!");
        System.out.println("Remember, you can type 'help' at anytime to view the commands!");
    }

    //If user inputs 'n' (no), print below.
    if (vCheck1.equals("n")) {
        System.out.println("You start off with $1000, and your goal is to cash out with as much money as possible.");
        System.out.println("Each time you cash out, your money will get sent to the high scores if it is within the top 5.");
        System.out.println("If your money goes to $0 or below, you will lose.");
        System.out.println("WARNING: Cashing out will reset the game.");
        System.out.println("You will be able to see the commands at any time, by typing 'commands'.");
        System.out.println("+--------------------------------------------------------------+");
        System.out.println("| This game is based on real-life roulette tables.             |");
        System.out.println("+--------------------------------------------------------------+");
        System.out.println("| Bet                    | Pays | Probability Win | House Edge |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Red                    | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Black                  | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Odd                    | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("|Even                    | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 1 to 18                | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 19 to 36               | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 1 to 12                | 2    | 31.58%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 13 to 24               | 2    | 31.58%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 25 to 36               | 2    | 31.58%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Six line (6 numbers)   | 5    | 15.79%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| First five (5 numbers) | 6    | 13.16           | 7.89%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Corner (4 numbers)     | 8    | 10.53%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Street (3 numbers)     | 11   | 7.895           | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Split (2 numbers)      | 17   | 5.26%           | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Any one number         | 35   | 2.62%           | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("SCROLL up for full instructions.");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

    }
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    // Setting up variables to be updated in the loop to be printed after game.
    String confirmationCashout = "";
    String confirmationRestart = "";
    String bet = "";
    String answer = "";
    int winStreak = 0;
    int gamble = 0;
    int payout = 0;
    int randomNum = 0;
    int money = 1000;
    int rounds = 1;
    boolean cashout = false;
    Random rand = new Random();

    CASHOUT_BREAK_OUT:
    while (money > 0) {

        payout = 0;
        System.out.println("Round " + rounds + ".");

        System.out.println("You have $" + money + ".");
        System.out.println("What would you like to do?");
        System.out.println("Commands: | bet | money | cashout | help | restart |");
        answer = input.next();
        while (!(answer.equals("bet")) && !(answer.equals("money")) && !(answer.equals("cashout")) &&
                !(answer.equals("restart")) && !(answer.equals("help"))) {
            System.out.println("Invalid choice, type 'help' to view the commands.");
            System.out.println("");
            System.out.println("What would you like to do?");
            System.out.println("Commands: | bet | money | cashout | help | restart |");
            answer = input.next();
        }

        while (answer.equals("money")) {
            System.out.println("Your balance is at $" + money + ".");
            System.out.println("What would you like to do?");
            System.out.println("Commands: | bet | money | cashout | help | restart |");
            answer = input.next();
        }

        while (answer.equals("cashout")) {
            System.out.println("Are you sure you want to cashout $" + money + "? Y/N.");
            confirmationCashout = input.next();
            while (!(confirmationCashout.equals("y")) && !(confirmationCashout.equals("n"))) {
                System.out.println("Please input either 'y' (yes) or 'n' (no). ");
                System.out.println("Are you sure you want to cashout $" + money + "? Y/N.");
                confirmationCashout = input.next();
            }
            if (confirmationCashout.equals("y")) {
                System.out.println("You have cashed out $" + money + " with a " +
                        winStreak + " win streak within " + rounds + " rounds.");
                break CASHOUT_BREAK_OUT;
            }
            if (confirmationCashout.equals("n")) {
                System.out.println("What would you like to do?");
                System.out.println("Commands: | bet | money | cashout | help | restart |");
                answer = input.next();
            }
        }

        while (answer.equals("restart")) {
            System.out.println("Are you sure you want to RESTART?");
            System.out.println("YOUR progress will be DELETED. Y/N.");
            confirmationRestart = input.next();
            if (!(confirmationRestart.equals("y")) && !(confirmationRestart.equals("n"))) {
                System.out.println("Please pick either Y/N.");
                System.out.println("Are you sure you want to RESTART?");
                confirmationRestart = input.next();
            }
            if (confirmationRestart.equals("y")) {
                System.out.println("Restarting...");
                money = 1000;
                winStreak = 0;
                rounds = 1;
                System.out.println("What would you like to do?");
                System.out.println("Commands: | bet | money | cashout | help | restart |");
                answer = input.next();
            }
            if (confirmationRestart.equals("n")) {
                System.out.println("What would you like to do?");
                System.out.println("Commands: | bet | money | cashout | help | restart |");
                answer = input.next();
            }
        }
        while (answer.equals("help")) {
            System.out.println("You start off with $1000, and your goal is to cash out with as much money as possible.");
            System.out.println("Each time you cash out, your money will get sent to the high scores if it is within the top 5.");
            System.out.println("If your money goes to $0 or below, you will lose.");
            System.out.println("WARNING: Cashing out will reset the game.");
            System.out.println("You will be able to see the commands at any time, by typing 'commands'.");
            System.out.println("+--------------------------------------------------------------+");
            System.out.println("| This game is based on real-life roulette tables.             |");
            System.out.println("+--------------------------------------------------------------+");
            System.out.println("| Bet                    | Pays | Probability Win | House Edge |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Red                    | 1    | 47.37%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Black                  | 1    | 47.37%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Odd                    | 1    | 47.37%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("|Even                    | 1    | 47.37%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| 1 to 18                | 1    | 47.37%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| 19 to 36               | 1    | 47.37%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| 1 to 12                | 2    | 31.58%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| 13 to 24               | 2    | 31.58%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| 25 to 36               | 2    | 31.58%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Six line (6 numbers)   | 5    | 15.79%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| First five (5 numbers) | 6    | 13.16           | 7.89%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Corner (4 numbers)     | 8    | 10.53%          | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Street (3 numbers)     | 11   | 7.895           | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Split (2 numbers)      | 17   | 5.26%           | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("| Any one number         | 35   | 2.62%           | 5.26%      |");
            System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("You should probably familiarize yourself with the layout of a roulette table" +
                    "before playing this command-line version of the game.");
            System.out.println("SCROLL up for full instructions.");
            System.out.println("What would you like to do?");
            try { Thread.sleep(5000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
            System.out.println("");
            System.out.println("");
            System.out.println("Commands: | bet | money | cashout | help | restart |");
            answer = input.next();
        }

        try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
        System.out.println("+--------------------------------------------------------------+");
        System.out.println("| This game is based on real-life roulette tables.             |");
        System.out.println("+--------------------------------------------------------------+");
        System.out.println("| Bet                    | Pays | Probability Win | House Edge |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Red                    | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Black                  | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Odd                    | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("|Even                    | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 1 to 18                | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 19 to 36               | 1    | 47.37%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 1 to 12                | 2    | 31.58%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 13 to 24               | 2    | 31.58%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| 25 to 36               | 2    | 31.58%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Six line (6 numbers)   | 5    | 15.79%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| First five (5 numbers) | 6    | 13.16           | 7.89%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Corner (4 numbers)     | 8    | 10.53%          | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Street (3 numbers)     | 11   | 7.895           | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Split (2 numbers)      | 17   | 5.26%           | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
        System.out.println("| Any one number         | 35   | 2.62%           | 5.26%      |");
        System.out.println("+------------------------+------+-----------------+------------+");
            System.out.println("");
            System.out.println("");
            System.out.println("Commands: | red     | black     | odd    | even   |   any | 1to18 | 19to36 |");
            System.out.println("          | sixline | firstfive | corner | street | split | 1to12 | 13to24 | 25to36 |");
            System.out.println("");
            System.out.println("What would you like to bet on?");
            //Validation check - Checks if user has input an invalid bet.
            bet = input.next();
            while (!(bet.equals("red")) && !(bet.equals("black")) && !(bet.equals("even")) &&
                    !(bet.equals("odd")) && !(bet.equals("1to18")) && !(bet.equals("19to36")) &&
                    !(bet.equals("1to12")) && !(bet.equals("13to24")) && !(bet.equals("25to36")) &&
                    !(bet.equals("sixline")) && !(bet.equals("firstfive")) && !(bet.equals("corner")) &&
                    !(bet.equals("street")) && !(bet.equals("split")) && !(bet.equals("any"))) {
                System.out.println("Invalid choice, check the table to view what you can bet on.");
                try { Thread.sleep(500); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("");
                System.out.println("Commands: | red     | black     | odd    | even   |   any | 1to18 | 19to36 |");
                System.out.println("          | sixline | firstfive | corner | street | split | 1to12 | 13to24 | 25to36 |");
                System.out.println("");
                System.out.println("What would you like to bet on?");
                bet = input.nextLine();
            }

            System.out.println("How much money are you going to chip in?");
            gamble = input.nextInt();
            while (gamble > money) {
                System.out.println("Nice try, you're betting more than you can handle...");
                try { Thread.sleep(2500); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("How much money are you going to chip in?");
                gamble = input.nextInt();
            }
            if (bet.equals("red") || bet.equals("black") || bet.equals("even") || bet.equals("odd") || bet.equals("1to18") || bet.equals("19to36")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble;
                if (randomNum < 4738) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 47.37%!");
                }
                if (randomNum > 4738) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 52.63%!");
                }
            } else if (bet.equals("1to12") || bet.equals("13to24") || bet.equals("25to36")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 2;
                if (randomNum < 3158) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 31.58%!");
                }
                if (randomNum > 3158) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 68.42%!");
                }
            } else if (bet.equals("sixline")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 5;
                if (randomNum < 1579) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 15.79%!");
                }
                if (randomNum > 1579) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 84.21%%!");
                }
            } else if (bet.equals("firstfive")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 6;
                if (randomNum < 1316) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 13.16%!");
                }
                if (randomNum > 1316) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 86.84%%!");
                }
            } else if (bet.equals("corner")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 8;
                if (randomNum < 1316) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 10.53%!");
                }
                if (randomNum > 1316) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 89.47%%!");
                }
            } else if (bet.equals("street")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 11;
                if (randomNum < 789) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 7.895%!");
                }
                if (randomNum > 789) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 92.105%!");
                }
            } else if (bet.equals("split")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 17;
                if (randomNum < 526) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 5.26%!");
                }
                if (randomNum > 526) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 94.74%!");
                }
            } else if (bet.equals("any")) {
                randomNum = rand.nextInt(10000) + 1;
                System.out.println("Betting $" + gamble + " on " + bet + "...");
                try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
                System.out.println("Spinning...");
                payout += gamble * 35;
                if (randomNum < 262) {
                    money += payout;
                    winStreak += 1;
                    System.out.println("You won $" + payout + " with 2.62%!");
                }
                if (randomNum > 262) {
                    money -= gamble;
                    winStreak = 0;
                    System.out.println("You lost $" + gamble + " with 97.38%!");
                }
            }

            if (money == 0) {
                System.out.println("You've gone broke!");
                System.exit(0);
            }

            System.out.println("You are on a " + winStreak + " win streak.");
            if (winStreak == 3) {
                System.out.println("You have been awarded $500 for your third win streak!");
                System.out.println("$500 has been deposited into your account.");
                money+= 500;
            }
            rounds += 1;
            System.out.println("");
            System.out.println("");
            try { Thread.sleep(2000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }


        }
    }
}

BallGenerator

import java.util.Random;

public class BallGenerator {
public RouletteBall generate() {

    RouletteBall ball = new RouletteBall();


    //Adds random to a variable then generates a number between 0 - 36.
    Random rand = new Random();
    int ballNumber = rand.nextInt(36);
    ball.setNumber(ballNumber);
    ball.setColour(determineColour(ballNumber));
    ball.setEven(oddOrEven(ballNumber));
    return ball;
}


private String determineColour(int numberLandedOn) {
    String colour;
    // If the ball number is 0, assign it green.
    if (numberLandedOn == 0) {
        colour = ("green " + numberLandedOn);
    }


    // If the ball number is even, assign it black.
    else if (numberLandedOn % 2 == 0) {
        colour = ("black " + numberLandedOn);
    }

    // If the ball number is odd, assign it red.
    else {
        colour = ("red " + numberLandedOn);

    }
    return colour;
}


private boolean oddOrEven(int numberLandedOn) {
    boolean even = false;
    if (numberLandedOn == 0) {
        even = true;
    }

    if (numberLandedOn % 2 == 0) {
        even = true;
    }

    if (numberLandedOn % 2 == 1) {
        even = false;
    }
    return even;
}

}

Roulette Ball

public class RouletteBall {
private int number;
private String colour;
private boolean even;

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public String getColour() {
    return colour;
}

public void setColour(String colour) {
    this.colour = colour;
}

public boolean isEven() {
    return even;
}

public void setEven(boolean even) {
    this.even = even;
}
}
\$\endgroup\$
3
\$\begingroup\$

Wow, that's quite a lot of code. I bet there are some repeating patterns in it. Because when I compress it using gzip -9, it shrinks to 11.24% of the original size. For comparison, sfntly shrinks to 16.49%, which is 50% larger.

Let's hunt for some repetitions.

The string 47.37% appears 19 times. 18 of these are in the table of probabilities, which is printed at three different places in the code. Since the text is exactly the same, you could extract this whole code into a method. This is something that your IDE (Eclipse, IntelliJ) can do for you. To do that, mark the code you want to extract and then press Ctrl+1 in Eclipse or Ctrl+Alt+M in IntelliJ. You are then asked to give a nice name to this new method. You should choose printProbabilitiesTable, since that is a short and precise description of what the method does.

You can apply this code transformation to many other places, but they are not as simple to transform. You need some manual work for that. One other example is the Thread.sleep that you are calling. Select the whole try { … } finally { … } block and extract that to a method called sleep. It should look like this now:

private static void sleep() {
    try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
}

There is one ugly thing, though. The new sleep method always sleeps for exactly 1000 milliseconds, but in some places in your code you want to sleep longer. Therefore, the sleep method should get a parameter. To see how this is written in Java, place the text cursor on the Thread.sleep method and press F3 in Eclipse or F4 in IntelliJ. This takes you to the definition of this method. You can see the following code:

public static native void sleep(long millis) throws InterruptedException;

Ok, this doesn't help much because of the native, since you cannot really see what this method is doing. Let's try another one. Place the cursor on the println of System.out.println("") and press F3/F4 again. You should land on the following code:

public void println(String x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

This tells you how to define a method that takes some parameter, in this case x, and how to use this parameter. (It's used exactly like an ordinary variable.) When we combine the two above methods, we can reach the following definition for our sleep method:

private static void sleep(long millis) {
    try { Thread.sleep(millis); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
}

Now you can replace all calls to Thread.sleep with calls to your own sleep method. This makes the code nicer to read since you don't need to write the try { … } anymore.

You can continue with the same idea and invent a method for letting the user choose from a predefined set of options. The skeleton of this method looks like this:

private static String choose(String... options) {
    …
}

This method would be called like this:

String answer = choose("yes", "no");
switch (answer) {
case "yes":
    // Do something here
    break;
case "no":
    // Do something else here
    break;
}

Or, if the String... options is too difficult still, just try the simple form:

private static boolean yesno(String prompt) {
    …
}

It would be used like this:

if (yesno("Do you want to put a bet on Red?")) {
    // Code for the Red bet
} else {
    // Code for the Black bet
}

Here's the full implementation of the choose method, if you want to cheat. I recommend you try it on your own, first.

https://codereview.stackexchange.com/a/180451

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I'll fix the code up, I know I can use some of these tips for future programs. Much appreciated! :-) \$\endgroup\$
    – Tyler
    Jan 26 '18 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.