Advertisement
Guest User

project 7

a guest
Mar 4th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /*
  2. * Project07.java
  3. *
  4. * A program that plays the dice game high/low
  5. * Used to practice breaking code up into methods.
  6. *
  7. * @author Christopher Lam
  8. *
  9. */
  10. package osu.cse1223;
  11. import java.util.Scanner;
  12.  
  13.  
  14. public class Project07 {
  15.  
  16.  
  17. public static void main(String[] args) {
  18. // Fill in the body
  19.  
  20. }
  21.  
  22.  
  23. // Given a Scanner and a current maximum amount of money, prompt the user for
  24. // an integer representing the number of dollars that they want to bet. This
  25. // number must be between 0 and to maximum number of dollars. If the user enters
  26. // a number that is out of bounds, display an error message and ask again.
  27. // Return the bet to the calling program.
  28. private static int getBet(Scanner inScanner, int currentPool) {
  29. // Fill in the body
  30. System.out.println("You have " + currentPool + " dollars.");
  31. System.out.println("Enter an amount to bet (0 to quit)");
  32. int bet = inScanner.nextInt();
  33. while (bet != 0)
  34. {
  35. if (bet < 0 || bet > currentPool)
  36. {
  37. System.out.println("Your bet MUST be between 0 and " + currentPool);
  38. System.out.println("You have " + currentPool + " dollars.");
  39. System.out.println("Enter an amount to bet (0 to quit)");
  40. bet = inScanner.nextInt();
  41. }
  42. else
  43. {
  44. System.out.println("You have " + currentPool + " dollars.");
  45. System.out.println("Goodbye!");
  46. }
  47. }
  48. return bet;
  49. }
  50.  
  51. // Given a Scanner, prompt the user for a single character indicating whether they
  52. // would like to bet High ('H'), Low ('L') or Sevens ('S'). Your code should accept
  53. // either capital or lowercase answers, but should display an error if the user attempts
  54. // to enter anything but one of these 3 values and prompt for a valid answer.
  55. // Return the character to the calling program.
  56. private static char getHighLow(Scanner inScanner) {
  57. // Fill in the body
  58. System.out.println("High, low or sevens (H/L/S): ");
  59. String choice = inScanner.nextLine();
  60. while (!choice.equals('H') || !choice.equals('h') || !choice.equals('L')
  61. || !choice.equals('l') || !choice.equals('S') || !choice.equals('s'))
  62. {
  63. System.out.println("Invalid chocie. Choose High, low, or sevens");
  64. System.out.println("High, low or sevens (H/L/S): ");
  65. choice = inScanner.nextLine();
  66. }
  67. return choice;
  68. }
  69.  
  70. // Produce a random roll of a single six-sided die and return that value to the calling
  71. // program
  72. private static int getRoll() {
  73. // Fill in the body
  74. int roll = (int)(6 * Math.random()) + 1;
  75. return roll;
  76. }
  77.  
  78. // Given the choice of high, low or sevens, the player's bet and the total result of
  79. // the roll of the dice, determine how much the player has won. If the player loses
  80. // the bet then winnings should be negative. If the player wins, the winnings should
  81. // be equal to the bet if the choice is High or Low and 4 times the bet if the choice
  82. // was Sevens. Return the winnings to the calling program.
  83. private static int determineWinnings(char highLow, int bet, int roll) {
  84. // Fill in the body
  85. int winnings;
  86. if (roll >= 8)
  87. {
  88. winnings = bet;
  89. }
  90. else if (roll <= 6)
  91. {
  92. winnings = bet;
  93. }
  94. else
  95. {
  96. winnings = 4 * bet;
  97. }
  98. return winnings;
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement