Advertisement
feagans

Untitled

Apr 20th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. /**
  2. Name: Kenneth Figgins
  3. Date: 04/20/14
  4. Course/Section: IT 106.010
  5. Assignment: Programming Assignment 8
  6.  
  7. Description:
  8.  
  9. This program is for a "price is right" game. Where there
  10. are four players who are all competing again each other
  11. to guess the closest without going over on a set item price
  12. for a total of three rounds. The winner of each round has an increasing
  13. payout while a consolation price is still paid for zero wins.
  14.  
  15. The guesses are input by the user and then the program calculates
  16. if all the players are over and it will repeat the round or it
  17. will find theplayer who is closest to the exact price. Upon this
  18. that play is given a round winand it continues like this for 3 rounds.
  19.  
  20. The output will be a list of all players, their rounds won,
  21. and the total winningsin dollars calculated as well.
  22. */
  23.  
  24. import javax.swing.JOptionPane;
  25.  
  26. public class assignment8 {
  27.  
  28. public static void main(String[] args) {
  29. int round = 1;
  30. int bustPlayers = 0;
  31.  
  32. while(round <= 3){
  33. double secretPrice = getSecretPrice();
  34. double[] playerGuess = getPlayerGuess();
  35.  
  36. for(int i = 0; i < playerGuess.length; i++){
  37. if (playerGuess[i] > secretPrice) {
  38. bustPlayers++;
  39. }
  40. String compareGuess = processCompareGuess(secretPrice, playerGuess);
  41. }
  42. if (bustPlayers == playerGuess.length) {
  43. continue;
  44. }
  45. String compareGuess = processCompareGuess(secretPrice, playerGuess);
  46. round++;
  47. }
  48. displayResults();
  49. }
  50.  
  51.  
  52. public static double getSecretPrice() {
  53. double secretPrice;
  54. return secretPrice = (Double.parseDouble(JOptionPane.showInputDialog("Please enter the secret price")));
  55. }
  56.  
  57. public static double[] getPlayerGuess() {
  58. double playerGuess[] = new double[4];
  59. for(int i = 0; i < playerGuess.length; i++){
  60. int player = (1 + i);
  61. if ( player == 1){
  62. player = 1;
  63. }
  64. //error handling to make sure the entry is a positive number and not a letter
  65. while (true){
  66. try{
  67. playerGuess[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Player"+player+", Please enter your guess"));
  68. if (playerGuess[i] < 0) {
  69. JOptionPane.showMessageDialog(null,"No Negative Entries");
  70. continue;
  71. }
  72. }
  73. catch (IllegalArgumentException e) {
  74. JOptionPane.showMessageDialog(null,"Entries Must Be Numbers");
  75. continue;
  76. }
  77. break;
  78. }
  79. for(int j = 0; j < i; j++) {
  80. if (playerGuess[i] == playerGuess[j]) {
  81. i--;
  82. playerGuess[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Player"+player+", Please enter a new guess, that guess already exists"));
  83. }
  84. }
  85. }
  86. return playerGuess;
  87. }
  88.  
  89. public static String processCompareGuess (double secretPrice, double[] playerGuess) {
  90.  
  91. double bestDistanceFoundYet = Double.MAX_VALUE;
  92. String instantWinner = null;
  93. String nearest = null;
  94. // We iterate on the array...
  95. for (int i = 0; i < playerGuess.length; i++) {
  96. // if we found the desired number, we return it.
  97. if (playerGuess[i] == secretPrice) {
  98. return instantWinner = Double.toString(playerGuess[i]);
  99. } else {
  100. // else, we consider the difference between the desired number and the current number in the array.
  101. double d = Math.abs(secretPrice - playerGuess[i]);
  102. if (d < bestDistanceFoundYet) {
  103. // For the moment, this value is the nearest to the desired number...
  104. nearest = ("Player" + i + "is the cloest guess with" + playerGuess[i]);
  105. d = bestDistanceFoundYet;
  106. }
  107. }
  108. }
  109. return nearest;
  110. }
  111.  
  112. public static void displayResults() {
  113.  
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement