Hello and welcome to our community! Is this your first visit?
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
    Apr 2009
    Thanked 0 Times in 0 Posts

    "For loop" question

    I have an assignment due for Uni which requires me to make a program that asks you if you want to play first and then if yes.. generates a random number between 1-100 and then allowing me to guess that number. But i also need to specify a limit of how many times i may attempt to guess.

    What i need the program to do is:
    1. if i cant guess the number within the limit to revert me back to the start of the program which asks me if i want to play.

    2. if i guess the number to revert me back to the start and ask if i want to play..

    I can get number 1 to work, but number 2 is the problem... for instance if i guess the number on my first try, the program will keep asking me to guess a number because the For loop is still true.. I dont know what variable to assign to it to make it false once the correct number is guessed.

     * Game to guess a random number between 1-100 with the amount of specified try's you want.
     * @author 
     * Date: 28/3/09
    import javax.swing.JOptionPane;
    public class GuessNumber
    {   public static void main (String [] args)
            String play    = JOptionPane.showInputDialog("Do you want to play the guessing game? (y/n)");                   
            String name    = JOptionPane.showInputDialog("Enter your first name");
            String surname = JOptionPane.showInputDialog("Enter your surname");
            String guess   = JOptionPane.showInputDialog("How many guesses do you want?");
            int randomnum = (int) (Math.random() * 2) + 1;  
            int guess1 = Integer.parseInt(guess);
            if (guess1 >= 21)
                JOptionPane.showMessageDialog (null, "That is too many guesses!");
            for (int count = 1; count <= guess1 && guess1 <21; count++)                    //creates a loop to allow only a certain number of guesses
            String number  = JOptionPane.showInputDialog("Guess a number between 1 and 100");
            int number1 = Integer.parseInt(number);  
                if ( count == guess1 && number1 != randomnum)                                      //stops the program with a message when all guesses have been used.
                    JOptionPane.showMessageDialog (null, "You have ran out of guesses!"+"\nThe number was "+randomnum);
                    play = JOptionPane.showInputDialog("Do you want to play the guessing game? (y/n)");
            String uppername    = name.toUpperCase();                                   
            String uppersurname = surname.toUpperCase();
            char ch;
            ch = uppername.charAt(0);                                                           //uses first letter of first name
            if (number1 != randomnum && count < guess1)                                        //pops up message when number is incorect
                JOptionPane.showMessageDialog (null, "That is incorrect, sorry.");
            if (number1 == randomnum)                                                          //pops up message when number is correct
                    JOptionPane.showMessageDialog (null, ch+" "+ uppersurname +" You got it!!"+"\nGood guess!!");
                    play = JOptionPane.showInputDialog("Do you want to play the guessing game? (y/n)"); 

  2. #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Orlando, FL
    Thanked 40 Times in 40 Posts
    Your first option, and probably what your teacher does not want you to do as it's generally frowned upon is to add a break statement after you ask if they want to play again. It'll break you out of the for loop.

    The best option (not for just your teacher, but in general coding practice) is not to use a for loop and break statement. I would use a while loop and do something like:
    while(count < guess1 && !correct) {
      // blah blah blah
    The variable correct would be a boolean storing false normally, but would be set to true if they get it right. The count variable would be incremented within the loop if they get a wrong guess.

    I would also either set guess1 to 21 if they choose too many tries, or put that part in its own loop and make it loop until they pick a valid number.



Posting Permissions

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