Member Avatar for loserspearl

I'm working on a random number guessing game with 5 tries that uses standard JOptionPane GUI and 3 methods, the requirements have each method doing a specific task.

The main method starts a game is supposed to handle the 'you won' or 'you lost' dialogue and play again if the user clicks ok and stop if they click cancel.

the playGame method plays a full game and returns a boolean value if the user wants to play another.

and the compareTo method compares the guess with the actual random answer and returns a 0 if true, potivie is guess was too, and negative is guess was too low.

I cannot see why I'm getting a compiler error from my joptionpane messages, the syntax seems correct. Would I need to make a global counter variable for won or lost option in the main method? either that or make a new boolean called win in the main? I've coded myself into maze. Any help is appreciated.

// Java Project 3 10/3/11
// Joe Owens
//  

import java.util.*;
import javax.swing.*;

public class GuessGame {
    public static void main ( String[] args ) {
/*	
This method is responsible for the "Play again?" logic, including the “you won/you lost” dialog.  
This means a loop that runs at least once, and continues until the player quits. 
*/
	boolean playAgain = false;
	boolean win = false;
	
	do {
 
		if (playGame(win))
			JOptionPane.showMessageDialog("You win, play again?.", "Choose one", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
		else
			JOptionPane.showMessageDialog("You lost, play again?.", "Choose one", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
	} while (playAgain == true);

    }
	
    static boolean playGame (boolean win) {

/* 
This method is responsible for playing one complete game each time it is called, 
including the “what is your guess?” input dialog.  Note the message displayed in the input dialog changes each time through the loop, to show if the user's last guess was too high or too low.  
The method should return true if the user won.  If they don't guess correctly after four tries, the user has lost and the method should return false. 
*/
	int chance = 4;			//chances total
	int guessCtr = 0;		//guesses made
	String guessStr;			 

	Random rand = new Random();
  	int answer = rand.nextInt(9) + 1;  //gives random number 0 to 9 and adds 1 to make 1-10.
	
	while ( guessCtr < chance ) {

		guessStr = JOptionPane.showInputDialog("Guess a number 1 to 10.", "" + chance + " chances left", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
		
		try{
			int guess = Integer.parseInt(guessStr);
		}
		catch (Exception ex) {
			//error handling for invalid inputs such as letters
			JOptionPane.showInputDialog("Invalid input, please try again.", "" + chance + " chances left", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
		}
		continue;	
		if ( compareTo( guess, answer ) == 0){
			JOptionPane.showMessageDialog("You are correct, well done!.", "Choose one", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
			return true;
			break;
        	} 
		else if ( compareTo( guess, answer ) < 0){
			JOptionPane.showInputDialog("Your guess was too low.", "" + chance + " chances left", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
			break;
		}	
		else if ( compareTo( guess, answer ) > 0){ //could make this else
			JOptionPane.showInputDialog("Your guess was too high.", "" + chance + " chances left", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
			break;
		}	
		guessCtr++;
	}
	return false;
    }

    static int compareTo ( int compGuess, int compAnswer ) {
	
/* This method compares the user input (a single guess) with the correct answer.  
It returns a negative integer if the guess is too low, a positive integer if the guess is too high, and 0 (zero) if the guess is correct
*/  
	
	//int result = Integer.compareTo( compGuess, compAnswer );
	//int result = compGuess.compareTo( compAnswer );
	//return result;
	
	//I was trying all 3 ways, built in compareTo both giving a compiler error
	
	if ( compGuess == compAnswer){
		return 0;
	    	
        } 
	else if ( compGuess < compAnswer){
		return -1;
		
	}	
	else if ( compGuess > compAnswer ){
		return 1;
		
	}
	
    }
}

Recommended Answers

All 3 Replies

If you look at API there's no showMessageDialog method which takes string as first argument.

Call the method by passing correct parameters.

Member Avatar for loserspearl

Got it to compile but it wont give more than once chance to play, and when I click ok to play again it closes. This is an issue with my main method, not sure how to use the confirmdialog to change playAgain to true

import java.util.*;
import javax.swing.*;

public class GuessGame {
    public static void main ( String[] args ) {

        boolean playAgain = false;
        boolean win = false;
	
        do {
            if (playGame(win) == true)	
                JOptionPane.showConfirmDialog(null, "You are correct! Play again?.", "Choose one", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            else
                JOptionPane.showConfirmDialog(null, "You lost, play again?.", "Choose one", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        } while (playAgain == true);
    }
	
    static boolean playGame (boolean win) {

        int chance = 4;			//chances total
        int guessCtr = 0;		//guesses made
        int guess = 0;
        String guessStr;			 
	
        Random rand = new Random();
        int answer = rand.nextInt(9) + 1;  //gives random number 0 to 9 and adds 1 to make 1-10.
		
        guessStr = JOptionPane.showInputDialog(null, "Guess a number 1 to 10.", "" + chance + " chances left", JOptionPane.QUESTION_MESSAGE);

	while ( guessCtr < chance ) {

            try {
                guess = Integer.parseInt(guessStr);
            }
                catch (Exception ex) {
                    //error handling for invalid inputs such as letters
                    JOptionPane.showInputDialog(null, "Invalid input, please try again.", "" + chance + " chances left", JOptionPane.ERROR_MESSAGE);
                    continue;
                }
                // calls compareTo to know if answer is correct, too high, or too low		
                if ( compareTo( guess, answer ) == 0)	
                    return true;
                else if ( compareTo( guess, answer ) < 0){
                    JOptionPane.showInputDialog(null, "Your guess was too low.", "" + chance + " chances left", JOptionPane.QUESTION_MESSAGE);
                    guessCtr++;
                    break;
                }	
                else {
                    JOptionPane.showInputDialog(null, "Your guess was too high.", "" + chance + " chances left", JOptionPane.QUESTION_MESSAGE);
                    guessCtr++; 
                    break;
                }	
        }
        return false;
    }

    static int compareTo ( int compGuess, int compAnswer ) {
	
        if ( compGuess == compAnswer)
            return 0; 	
        else if ( compGuess < compAnswer)
            return -1;
	else
            return 1;   
    }

}
Member Avatar for loserspearl

got the play again to work with

int playAgain = 0;
        boolean win = false; 
	
        do {
            if (playGame(win) == false)	
                playAgain = JOptionPane.showConfirmDialog(null, "You lost, play again?.", "Choose one", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            else
                playAgain = JOptionPane.showConfirmDialog(null, "You are correct! Play again?.", "Choose one", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        } while (playAgain == 0);
    }

but my guess counter loop isnt running through more than 1 chance. Also if it throws an exception once its stuck in the exception forever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.