Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2017
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Number Guessing Game Help

    Help on Number Guesser Code
    Im struggling so much with this assignment. The requirements are as follows
    Hint to display number trying to be guessed
    Counter to tell how many times they guessed
    Custom images in JOptionPane


    Had it working fine until i tried to add the images and now idk whats wrong with it

    Any help would be greatly appreciated!



    Code:
    // Program plays guess the number.
    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import javax.swing.ImageIcon;
    
    
    public class Guess
    {
    private static final Random randomNumbers = new Random();
    private static int answer; // the answer to be guessed
    private static boolean gameOver = false; // whether the game is over
    private static int numTries =0;// try counter
    private static String tries = "";
    private static int userGuess = 0;
    private static ImageIcon icon1 = new ImageIcon ("CORRECTFINAL.JPG");
    private static ImageIcon icon2 = new ImageIcon ("DOWNFINAL.JPG");
    private static ImageIcon icon3 = new ImageIcon ("UPFINAL.JPG");
    private static ImageIcon icon4 = new ImageIcon ("GUESSFINAL.JPG");
    private static ImageIcon icon5 = new ImageIcon ("HINTFINAL.JPG");
    
    // play games of guess the number
    public static void main( String args[] )
    {
    Scanner input = new Scanner( System.in ); 
    
    while ( !gameOver )
    {
    newGame(); // start a new game
    
    do
    {
    userGuess = Interger.parseInt (JOptionPane.showInputDialogue( "Guess (1 - 1000): ", "O" )); 
    checkUserGuess( userGuess);
    } while ( !gameOver );
    
    if ( gameOver )
    {
    int PlayAgain = Interger.parseInt(JOptionPane.showInputDialogue( "Play again (1=yes, 2=no)? " )
    );
    int playAgain = input.nextInt();
    
    // if playAgain is 1, set gameOver back to false
    if ( playAgain == 1 )
    gameOver = false;
    } // end if
    } // end while
    } // end main
    
    // create a new number to guess
    public static int getNumber()
    { 
    return 1 + randomNumbers.nextInt( 1000 );
    } // end method getNumber
    
    // starts a new game
    public static void newGame()
    {
    answer = getNumber();
    JOptionPane.showMessageDialog(null, answer, JOptionPane.INFORMATION_MESSAGE, null, "");
    JOptionPane.showMessageDialog(null, "\nGuess a number between 1 and 1000" );
    } // end method newGame
    
    // checks user input
    public static void checkUserGuess( int userGuess )
    {
    if ( userGuess < answer )
    {numTries++;
    
    JOptionPane.showMessageDialog(null, guess,+ userGuess + " is too low. Try again.", JOptionPane.INFORMATION_MESSAGE,icon2, null,"" );
    }
    else if ( userGuess > answer )
    {numTries++;
    
    JOptionPane.showMessageDialog(null, guess, + userGuess + " is too high. Try again.", JOptionPane.INFORMATION_MESSAGE, icon3, null,"" );
    }
    
    else
    {numTries++;
    gameOver = true; // game is over
    if (numTries == 1)
    {tries = " try ";
    }
    else
    {tries = "tries";
    }
    
    JOptionPane.showMessageDialog(null, "BINGO RIGHT ON THE MONEY\n", JOptionPane.INFORMATION_MESSAGE, icon1, null,"" );
    JOptionPane.showMessageDialog(null, "Congratulations. You guessed in " + numTries + " tries)", "CONGRADULATIONS" ,JOptionPane.INFORMATION_MESSAGE ); // end else
    } // end method checkUserGuess
    }
    } // end class Guess
    Last edited by vinyl-junkie; 02-16-2017 at 08:11 AM. Reason: added code tags

  2. #2
    New Coder
    Join Date
    Sep 2016
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    Because this is homework, the forum rules clearly state that I cannot help you. But I believe I could give you some pointers. When a variable has the 'final' keyword, the variable name should be in capitals, to show that the variable cannot change. Also, make sure to properly indent your code, it makes it look cleaner and more professional (I think...). And read this for the images error on the 'JOptionPane':

    https://docs.oracle.com/javase/7/doc...ptionPane.html


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •