public class RandomGame { private int numAttempts; private int guess; public RandomGame() { int difficulty; int rndNum; // specify difficulty String difficultyString = JOptionPane.showInputDialog("Enter a difficulty:"); difficulty = Integer.parseInt(difficultyString); // generate random # rndNum = (int)((Math.random() * difficulty) + 1); doUserGuess(difficulty, rndNum); displayResults(rndNum); } public void displayResults(int rndNum) { // display results String displayMsg; if (guess == 0) { displayMsg = "You gave up - number was: " + rndNum; } else { displayMsg = "Good guess - you got it!"; } displayMsg += "\nYou guessed " + numAttempts + " time(s)"; JOptionPane.showMessageDialog(null, displayMsg, null, JOptionPane.INFORMATION_MESSAGE); } public void doUserGuess(int difficulty, int rndNum) { // ask user boolean correct = false; boolean stopped = false; guess = 0; numAttempts = 0; // initialize array of guesses do { // ask user for a guess String inputValue = JOptionPane.showInputDialog("Enter a value between 1 and " + difficulty + " (0 to quit):"); guess = Integer.parseInt(inputValue); if (guess == 0) { stopped = true; } else { if (guess == rndNum) { correct = true; } else { // check guesses and if incorrect, tell user // else, not guessed - so Give user hint } numAttempts++; } } while (!correct && !stopped); } static public void main(String[] args) { new RandomGame(); System.exit(0); } }