Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class assignment8 {
- public static void main(String[] args) {
- double secretPrice = getSecretPrice();
- double[] playerGuess = getPlayerGuess();
- double compareGuesses = processCompareGuess();
- displayResults();
- }
- public static double getSecretPrice() {
- double secretPrice;
- secretPrice = (Double.parseDouble(JOptionPane.showInputDialog("Please enter the secret price")));
- }
- public static double[] getPlayerGuess() {
- double playerGuess[] = new double[3];
- for(int i = 0; i < 3; i++){
- int player = (1 + i);{
- if ( player == 1){
- player = 1;
- }
- //error handling to make sure the entry is a positive number and not a letter
- while (true){
- try{
- playerGuess[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Player"+player+", Please enter your guess"));
- if (playerGuess[i] < 0) {
- JOptionPane.showMessageDialog(null,"No Negative Entries");
- continue;
- }
- }
- catch (IllegalArgumentException e) {
- JOptionPane.showMessageDialog(null,"Entries Must Be Numbers");
- continue;
- }
- break;
- }
- for(int j = 0; j < 1; j++{
- if (playerGuess == playerGuess[j]) {
- i--;
- playerGuess[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Player"+player+", Please enter a new guess, that guess already exists"))
- }
- }
- return playerGuess;
- }
- }
- public static double processCompareGuesses(double secretPrice, double[] playerGuess) {
- int nearest = -1;
- int bestDistanceFoundYet = Integer.MAX_INTEGER;
- // We iterate on the array...
- for (int i = 0; i < playerGuess.length; i++) {
- // if we found the desired number, we return it.
- if (playerGuess[i] == secretPrice) {
- return playerGuess[i];
- } else {
- // else, we consider the difference between the desired number and the current number in the array.
- int d = Math.abs(desiredNumber - array[i]);
- if (d < bestDistanceFoundYet) {
- // For the moment, this value is the nearest to the desired number...
- nearest = playerGuess[i];
- }
- }
- }
- return nearest;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement