Advertisement
feagans

Untitled

Apr 20th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class assignment8 {
  4.  
  5. public static void main(String[] args) {
  6. int round = 1;
  7. int bustPlayers = 0;
  8.  
  9. while(round < 4){
  10. double secretPrice = getSecretPrice();
  11. double[] playerGuess = getPlayerGuess();
  12.  
  13. for(int i = 0; i < playerGuess.length; i++){
  14. if (playerGuess[i] > secretPrice) {
  15. bustPlayers++;
  16. }
  17. String compareGuess = processCompareGuess(secretPrice, playerGuess);
  18. }
  19. if (bustPlayers == playerGuess.length) {
  20. JOptionPane.showMessageDialog("All players have busted, round will replay");
  21. continue;
  22. }
  23. String compareGuess = processCompareGuess(secretPrice, playerGuess);
  24. round++;
  25. }
  26. displayResults(nearest, playerGuess);
  27. }
  28.  
  29.  
  30. public static double getSecretPrice() {
  31. double secretPrice;
  32. return secretPrice = (Double.parseDouble(JOptionPane.showInputDialog("Please enter the secret price")));
  33. }
  34.  
  35. public static double[] getPlayerGuess() {
  36. double playerGuess[] = new double[3];
  37. for(int i = 0; i < playerGuess.length; i++){
  38. int player = (1 + i);{
  39. if ( player == 1){
  40. player = 1;
  41. }
  42. //error handling to make sure the entry is a positive number and not a letter
  43. while (true){
  44. try{
  45. playerGuess[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Player"+player+", Please enter your guess"));
  46. if (playerGuess[i] < 0) {
  47. JOptionPane.showMessageDialog(null,"No Negative Entries");
  48. continue;
  49. }
  50. }
  51. catch (IllegalArgumentException e) {
  52. JOptionPane.showMessageDialog(null,"Entries Must Be Numbers");
  53. continue;
  54. }
  55. break;
  56. }
  57. for(int j = 0; j < i; j++) {
  58. if (playerGuess[i] == playerGuess[j]) {
  59. i--;
  60. playerGuess[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Player"+player+", Please enter a new guess, that guess already exists"));
  61. }
  62. }
  63. }
  64. }
  65. return playerGuess;
  66. }
  67.  
  68. public static String processCompareGuess (double secretPrice, double[] playerGuess) {
  69.  
  70. double bestDistanceFoundYet = Double.MAX_VALUE;
  71. String instantWinnerMessage;
  72. String instantWinner = null;
  73. String nearest = null;
  74. // We iterate on the array...
  75. for (int i = 0; i < playerGuess.length; i++) {
  76. // if we found the desired number, we return it.
  77. if (playerGuess[i] == secretPrice) {
  78. return instantWinner = Double.toString(playerGuess[i]);
  79. } else {
  80. // else, we consider the difference between the desired number and the current number in the array.
  81. double d = Math.abs(secretPrice - playerGuess[i]);
  82. if (d < bestDistanceFoundYet) {
  83. // For the moment, this value is the nearest to the desired number...
  84. nearest = ("Player" + i + "is the cloest guess with" + playerGuess[i]);
  85. d = bestDistanceFoundYet;
  86. }
  87. }
  88. }
  89. return nearest;
  90. }
  91.  
  92. public static void displayResults(String nearest, double[] playerGuess) {
  93. JOptionPane.showMessageDialog(null, nearest);
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement