Close

Results 1 to 3 of 3
  1. #1
    DF Rookie boabby9's Avatar
    Join Date
    Nov 2006
    Location
    Scotland
    Posts
    1
    Thanks
    0
    Thanked:        0
    Karma Level
    0

    Default Help me with my JAVA

    Can anyone help me with this piece of Java. I need to create a random number game where the user has three guesses at a number between 1 and 10. When running the game, if the user has guessed incorrectly 3 times, the message saying they have lost does not appear. Also, if they guess correctly on their first guess, it still asks them to guess and then the message comes up at the end stating they have had too many guesses. Please let me know if anyone can help me out with this.

    PHP Code:
    import javax.swing.JOptionPane;
     
    public class 
    RandomNumberGameQ1{
     public static 
    void main (String args[]){ 
      
    String userGuess1,userGuess2,userGuess3;
      
    int number1,number2,number3,randomNumber;
      
    String output="";
      
    boolean match=false;  
      
      
    //Generate a random number between 1 and 10
      
    randomNumber = (int) (Math.random() * 10) + 1;
       
      
    //Prompt user for their first guess
      
    userGuess1 JOptionPane.showInputDialog("Please guess a number between 1 and 10");
      
      
    //Convert string to integer
      
    number1 Integer.parseInt(userGuess1);
      
      
    //Compare users first guess to random number
       
    match checkGuess(number1randomNumber);
      if (
    match == true){
       
    output "You Won, The random number was " randomNumber;
       
       
    //print message
       
    JOptionPane.showMessageDialog(nulloutput"Random number result"
       
    JOptionPane.INFORMATION_MESSAGE);
       
      }
    //End if
      
      
    else {
       
    output "Try Again";
       
       
    //print message
       
    JOptionPane.showMessageDialog(nulloutput"Random number result"
       
    JOptionPane.INFORMATION_MESSAGE);
       }
    //End else
      
      
    if (match == false) {    
      
      
    //Prompt user for their second guess
      
    userGuess2 JOptionPane.showInputDialog("Please guess another number between 1 and 10");
      
      
    //Convert string to integer
      
    number2 Integer.parseInt(userGuess2);
      
      
    //Compare users second guess to random number
       
    match checkGuess(number2,randomNumber);
      if (
    match == true){
       
    output "You Won, The random number was " randomNumber;
       
       
    //print message
       
    JOptionPane.showMessageDialog(nulloutput"Random number result"
       
    JOptionPane.INFORMATION_MESSAGE);
       }
    //End if
       
       
    }//End if
      
    else{
       
    output "Try Again";
       
       
    //print message
       
    JOptionPane.showMessageDialog(nulloutput"Random number result"
       
    JOptionPane.INFORMATION_MESSAGE);
       }
    //End else
      
    if (match == false) {    
      
      
    //Prompt user for their Final guess
      
    userGuess3 JOptionPane.showInputDialog("Please guess your final number between 1 and 10");
      
      
    //Convert string to integer
      
    number3 Integer.parseInt(userGuess3);
      
      
    //Compare users final guess to random number
       
    match checkGuess(number3,randomNumber);
      if (
    match == true){
       
    output "You Won, The random number was " randomNumber;
       
       
    //print message
       
    JOptionPane.showMessageDialog(nulloutput"Random number result"
       
    JOptionPane.INFORMATION_MESSAGE);
       }
    //End if
       
       
    }//End if
      
    else{
       
    output "Sorry, You have guessed incorrectly too many times. The random number was " randomNumber;
       
       
    //print message
       
    JOptionPane.showMessageDialog(nulloutput"Random number result"
       
    JOptionPane.INFORMATION_MESSAGE);
      }
    //End else 
      
      
    }//End main
      
      //Method to check the users guess to the random number
      
    public static boolean checkGuess(int userGuessint randomNumber){
       
        
    boolean match false;
       
       if (
    userGuess == randomNumber)
       
    match true;
       
       return 
    match;
       
       }
    //End method
     
      


  2. #2
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    546

    Default Re: Help me with my JAVA

    haven't read your code but for the guesses part I'd have something like

    Code:
    while(notGuessedCorrectly && count <3)
      getUserGuess();
    
    if (!notGuessedCorrectly)
      winMessage();
    else
      loseMessage();
    in the getUserGuess bit you then have the input, call a method that checks wether it is right or not (changes the notGuessedCorrectly if correct) and increments the count.

    edit

    after looking at the code you are going to find it much easier with the random no. and the boolean for a correct guess as global variables and having seperate methods for getting the guess and one for running the game (with that little while loop in) saves code duplication.

    so something like

    main method

    set random no.
    calls game method

    game method

    bit of code I gave above

    get no. method

    gets no. from user and calls check method

    checkGuess method

    as you have

    win message method and lose method message - or maybe have them within main method when it returns or within the game method.
    Last edited by blacksheep; 22nd November 2006 at 01:47 PM.

  3. #3
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    546

    Default Re: Help me with my JAVA

    oh and its generally considered polite to at least post in the intro section before asking for help but welcome to the forum

Similar Threads

  1. J2ME / JBlend Java on Sharp GX10 ???
    By hummmm in forum Programming
    Replies: 0
    Last Post: 25th November 2002, 01:45 AM
  2. Java Popup's :()
    By Dragoncity in forum The Dog and Duck
    Replies: 0
    Last Post: 7th October 2002, 04:16 PM
  3. Java console??
    By Jaffa in forum PC Problems
    Replies: 3
    Last Post: 26th September 2002, 02:14 AM
  4. Java Idea?
    By Roo in forum Unlocking Questions & Solutions
    Replies: 0
    Last Post: 8th September 2002, 06:55 PM

Social Networking Bookmarks

Posting Permissions

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