Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project07.java
- *
- * A program that plays the dice game high/low
- * Used to practice breaking code up into methods.
- *
- * @author Christopher Lam
- *
- */
- package osu.cse1223;
- import java.util.Scanner;
- public class Project07 {
- public static void main(String[] args) {
- // Fill in the body
- }
- // Given a Scanner and a current maximum amount of money, prompt the user for
- // an integer representing the number of dollars that they want to bet. This
- // number must be between 0 and to maximum number of dollars. If the user enters
- // a number that is out of bounds, display an error message and ask again.
- // Return the bet to the calling program.
- private static int getBet(Scanner inScanner, int currentPool) {
- // Fill in the body
- System.out.println("You have " + currentPool + " dollars.");
- System.out.println("Enter an amount to bet (0 to quit)");
- int bet = inScanner.nextInt();
- while (bet != 0)
- {
- if (bet < 0 || bet > currentPool)
- {
- System.out.println("Your bet MUST be between 0 and " + currentPool);
- System.out.println("You have " + currentPool + " dollars.");
- System.out.println("Enter an amount to bet (0 to quit)");
- bet = inScanner.nextInt();
- }
- else
- {
- System.out.println("You have " + currentPool + " dollars.");
- System.out.println("Goodbye!");
- }
- }
- return bet;
- }
- // Given a Scanner, prompt the user for a single character indicating whether they
- // would like to bet High ('H'), Low ('L') or Sevens ('S'). Your code should accept
- // either capital or lowercase answers, but should display an error if the user attempts
- // to enter anything but one of these 3 values and prompt for a valid answer.
- // Return the character to the calling program.
- private static char getHighLow(Scanner inScanner) {
- // Fill in the body
- System.out.println("High, low or sevens (H/L/S): ");
- String choice = inScanner.nextLine();
- while (!choice.equals('H') || !choice.equals('h') || !choice.equals('L')
- || !choice.equals('l') || !choice.equals('S') || !choice.equals('s'))
- {
- System.out.println("Invalid chocie. Choose High, low, or sevens");
- System.out.println("High, low or sevens (H/L/S): ");
- choice = inScanner.nextLine();
- }
- return choice;
- }
- // Produce a random roll of a single six-sided die and return that value to the calling
- // program
- private static int getRoll() {
- // Fill in the body
- int roll = (int)(6 * Math.random()) + 1;
- return roll;
- }
- // Given the choice of high, low or sevens, the player's bet and the total result of
- // the roll of the dice, determine how much the player has won. If the player loses
- // the bet then winnings should be negative. If the player wins, the winnings should
- // be equal to the bet if the choice is High or Low and 4 times the bet if the choice
- // was Sevens. Return the winnings to the calling program.
- private static int determineWinnings(char highLow, int bet, int roll) {
- // Fill in the body
- int winnings;
- if (roll >= 8)
- {
- winnings = bet;
- }
- else if (roll <= 6)
- {
- winnings = bet;
- }
- else
- {
- winnings = 4 * bet;
- }
- return winnings;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement