Advertisement
feagans

Untitled

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