Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2009, 06:02 PM   PM User | #1
bokz06
New to the CF scene

 
Join Date: Apr 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bokz06 is an unknown quantity at this point
"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.


Code:
/**
 * 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)");                   
        while(play.equalsIgnoreCase("y"))
        {    
        
        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)"); 
                           
            }
        }
        }
    
        System.exit(0);
        
     
        
    }
}
bokz06 is offline   Reply With Quote
Old 04-02-2009, 06:19 PM   PM User | #2
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
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:
Code:
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.

-Shane
TheShaner is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:47 PM.


Advertisement
Log in to turn off these ads.