Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-28-2009, 04:37 AM
SwEeTAcTioN's Avatar
Member
 
Join Date: Oct 2009
Posts: 69
Rep Power: 0
SwEeTAcTioN is on a distinguished road
Exclamation Help on Do-While loop
So everything works in this number guessing game i wrote except for the do-while loop. When it asks you if you want to play again when you type n it works and displays "Thank You For Playing" but when you type in y it doesnt do anything. Can anyone tell me whats wrong and how to fix it?

Sorry the codes a little long

All help is appriecated very much

Code:
import java.io.*;
import javax.swing.*;
import java.util.Random;

public class GameProject{


  
  public static void main(String[] args)throws IOException{
  

  	BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
  	Random rand= new Random();
    int random=rand.nextInt(50)+1;
    boolean playAgain=false;
    char response;
    int guess1,guess2,guess3,guess4,guess5,guess6;
    
    System.out.println("Guess a Number Between 1-50");
    System.out.println("You will have 6 guesses");
    System.out.println("I will tell you if you need to go higher or lower");
    System.out.println("Good Luck");
    System.out.println("Guess #1");
    guess1=Integer.parseInt(input.readLine());
    if(guess1==random)
    {
    	System.out.println("You won");
    	System.out.println("Would you like to play again?");
    	response=input.readLine().charAt(0);
    	if(response=='Y'||response=='y')
    		playAgain=true;
    	if (response=='N'||response=='n')
    		System.out.println("Thank You For Playing!");
    } 
    
     
    if(guess1<random)
    	System.out.println("Go higher");
    if(guess1>random)
    	System.out.println("Go lower");
    if(guess1<random||guess1>random)
   	System.out.println("Please Guess Again");
   	 if(guess1<random||guess1>random)
   	System.out.println("Guess #2");
    guess2=Integer.parseInt(input.readLine());
    if(guess2==random)
    	{
    	System.out.println("You won");
    	System.out.println("Would you like to play again?");
    	response=input.readLine().charAt(0);
    	if(response=='Y'||response=='y')
    		playAgain=true;
    	if (response=='N'||response=='n')
    		System.out.println("Thank You For Playing!");
        } 
    

    if(guess2<random)
    	System.out.println("Go higher");
    if(guess2>random)
    	System.out.println("Go lower");
   if(guess2<random||guess2>random)
   	System.out.println("Please Guess Again");
   	if(guess2<random||guess2>random)
   	System.out.println("Guess #3");
    guess3=Integer.parseInt(input.readLine());
    if(guess3==random)
    	{
    	System.out.println("You won");
    	System.out.println("Would you like to play again?");
    	response=input.readLine().charAt(0);
    	if(response=='Y'||response=='y')
    		playAgain=true;
    	if (response=='N'||response=='n')
    		System.out.println("Thank You For Playing!");
        } 
    	
   
    if(guess3<random)
    	System.out.println("Go higher");
    if(guess3>random)
    	System.out.println("Go lower");
   if(guess3<random||guess3>random)
   	System.out.println("Please Guess Again");
   	if(guess3<random||guess3>random)
   	System.out.println("Guess #4");
    guess4=Integer.parseInt(input.readLine());
    if(guess4==random)
    	{
    	System.out.println("You won");
    	System.out.println("Would you like to play again?");
    	response=input.readLine().charAt(0);
    	if(response=='Y'||response=='y')
    		playAgain=true;
    	if (response=='N'||response=='n')
    		System.out.println("Thank You For Playing!");
        }  
    	
    
    if(guess4<random)
    	System.out.println("Go higher");
    if(guess4>random)
    	System.out.println("Go lower");
    if(guess4<random||guess4>random)
   	System.out.println("Please Guess Again");
   	 if(guess4<random||guess4>random)
   	System.out.println("Guess 5");
    guess5=Integer.parseInt(input.readLine());
    if(guess5==random)
    	{
    	System.out.println("You won");
    	System.out.println("Would you like to play again?");
    	response=input.readLine().charAt(0);
    	if(response=='Y'||response=='y')
    		playAgain=true;
    	if (response=='N'||response=='n')
    		System.out.println("Thank You For Playing!");
        } 
    
    if(guess5<random)
    	System.out.println("Go higher");
    if(guess5>random)
    	System.out.println("Go lower");
    if(guess5<random||guess5>random)
   	System.out.println("Please Guess Again");
   	 if(guess5<random||guess5>random)
   	System.out.println("Guess 6");
    guess6=Integer.parseInt(input.readLine());
    if(guess6==random)
    	{
    	System.out.println("You won");
    	System.out.println("Would you like to play again?");
    	response=input.readLine().charAt(0);
    	if(response=='Y'||response=='y')
    		playAgain=true;
    	if (response=='N'||response=='n')
    		System.out.println("Thank You For Playing!");
        } 
    else
    {
   	System.out.println("You lose");
   	System.out.println("The number was: "+random);
   	if(playAgain==true)
   	 do{
   	
   	 	System.out.println("Good Luck");
    System.out.println("Guess #1");
    guess1=Integer.parseInt(input.readLine());
    if(guess1==random)
    	System.out.println("You won");
    if(guess1<random)
    	System.out.println("Go higher");
    if(guess1>random)
    	System.out.println("Go lower");
    if(guess1<random||guess1>random)
   	System.out.println("Please Guess Again");
   	 if(guess1<random||guess1>random)
   	System.out.println("Guess #2");
    guess2=Integer.parseInt(input.readLine());
    if(guess2==random)
    	System.out.println("You won");

    if(guess2<random)
    	System.out.println("Go higher");
    if(guess2>random)
    	System.out.println("Go lower");
   if(guess2<random||guess2>random)
   	System.out.println("Please Guess Again");
   	if(guess2<random||guess2>random)
   	System.out.println("Guess #3");
    guess3=Integer.parseInt(input.readLine());
    if(guess3==random)
    	System.out.println("You won");
   
    if(guess3<random)
    	System.out.println("Go higher");
    if(guess3>random)
    	System.out.println("Go lower");
   if(guess3<random||guess3>random)
   	System.out.println("Please Guess Again");
   	if(guess3<random||guess3>random)
   	System.out.println("Guess #4");
    guess4=Integer.parseInt(input.readLine());
    if(guess4==random)
    	System.out.println("You won");
    
    if(guess4<random)
    	System.out.println("Go higher");
    if(guess4>random)
    	System.out.println("Go lower");
    if(guess4<random||guess4>random)
   	System.out.println("Please Guess Again");
   	 if(guess4<random||guess4>random)
   	System.out.println("Guess 5");
    guess5=Integer.parseInt(input.readLine());
    if(guess5==random)
    	System.out.println("You won");
    if(guess5<random)
    	System.out.println("Go higher");
    if(guess5>random)
    	System.out.println("Go lower");
    if(guess5<random||guess5>random)
   	System.out.println("Please Guess Again");
   	 if(guess5<random||guess5>random)
   	System.out.println("Guess 6");
    guess6=Integer.parseInt(input.readLine());
    if(guess6==random)
    	System.out.println("You won");
    else
    {
   	System.out.println("You lose");
   	System.out.println("The number was: "+random);
    }
   	 }
    while(playAgain==true);
    
         
   	 }
    }
  }
__________________
Are you suggesting that Cocunuts migrate?!! -Monty Python
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-28-2009, 05:07 AM
Member
 
Join Date: Oct 2009
Posts: 3
Rep Power: 0
curious is on a distinguished road
Default
Here's the correct version of the code. You were not use flag (playAgain) correctly and you were not using the concept of loop correctly. if you copying the same code two times then you are not using loop.

Hope this helps.

Code:
import java.io.*;
import javax.swing.*;
import java.util.Random;

public class GameProject{


  
  public static void main(String[] args)throws IOException{
  

    BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
    Random rand= new Random();
    int random=rand.nextInt(50)+1;
    boolean playAgain=true;
    char response;
    int guess1,guess2,guess3,guess4,guess5,guess6;
    while (playAgain)
    {
    System.out.println("Guess a Number Between 1-50");
    System.out.println("You will have 6 guesses");
    System.out.println("I will tell you if you need to go higher or lower");
    System.out.println("Good Luck");
    System.out.println("Guess #1");
    guess1=Integer.parseInt(input.readLine());
    if(guess1==random)
    {
        System.out.println("You won");
        System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n'){
            playAgain=false;
            System.out.println("Thank You For Playing!");
        }
    } 
    
     
    if(guess1<random)
        System.out.println("Go higher");
    if(guess1>random)
        System.out.println("Go lower");
    if(guess1<random||guess1>random)
    System.out.println("Please Guess Again");
     if(guess1<random||guess1>random)
    System.out.println("Guess #2");
    guess2=Integer.parseInt(input.readLine());
    if(guess2==random)
        {
        System.out.println("You won");
        System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n'){
            playAgain=false;
            System.out.println("Thank You For Playing!");
        }
        } 
    

    if(guess2<random)
        System.out.println("Go higher");
    if(guess2>random)
        System.out.println("Go lower");
   if(guess2<random||guess2>random)
    System.out.println("Please Guess Again");
    if(guess2<random||guess2>random)
    System.out.println("Guess #3");
    guess3=Integer.parseInt(input.readLine());
    if(guess3==random)
        {
        System.out.println("You won");
        System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n'){
            playAgain=false;
            System.out.println("Thank You For Playing!");
        }
        } 
        
   
    if(guess3<random)
        System.out.println("Go higher");
    if(guess3>random)
        System.out.println("Go lower");
   if(guess3<random||guess3>random)
    System.out.println("Please Guess Again");
    if(guess3<random||guess3>random)
    System.out.println("Guess #4");
    guess4=Integer.parseInt(input.readLine());
    if(guess4==random)
        {
        System.out.println("You won");
        System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n')
        {
            playAgain=false;
            System.out.println("Thank You For Playing!");
        }
        }  
        
    
    if(guess4<random)
        System.out.println("Go higher");
    if(guess4>random)
        System.out.println("Go lower");
    if(guess4<random||guess4>random)
    System.out.println("Please Guess Again");
     if(guess4<random||guess4>random)
    System.out.println("Guess 5");
    guess5=Integer.parseInt(input.readLine());
    if(guess5==random)
        {
        System.out.println("You won");
        System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n'){
            playAgain=false;
            System.out.println("Thank You For Playing!");
        }
        } 
    
    if(guess5<random)
        System.out.println("Go higher");
    if(guess5>random)
        System.out.println("Go lower");
    if(guess5<random||guess5>random)
    System.out.println("Please Guess Again");
     if(guess5<random||guess5>random)
    System.out.println("Guess 6");
    guess6=Integer.parseInt(input.readLine());
    if(guess6==random)
        {
        System.out.println("You won");
        System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n')
        {playAgain=false;
            System.out.println("Thank You For Playing!");
            
        }
        } 
    
    
    else
    {
    System.out.println("You lose");
    System.out.println("The number was: "+random);
    System.out.println("Would you like to play again?");
        response=input.readLine().charAt(0);
        if(response=='Y'||response=='y')
            playAgain=true;
        if (response=='N'||response=='n')
        {playAgain=false;
            System.out.println("Thank You For Playing!");
        }
       
    }
}
}
  }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-29-2009, 03:19 AM
SwEeTAcTioN's Avatar
Member
 
Join Date: Oct 2009
Posts: 69
Rep Power: 0
SwEeTAcTioN is on a distinguished road
Default
Its still not replaying the game if you type in y and now n doesnt do anything
__________________
Are you suggesting that Cocunuts migrate?!! -Monty Python
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-29-2009, 03:35 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,461
Rep Power: 11
Fubarable is on a distinguished road
Default
You may get more help if you format your code better. As displayed, it's difficult to see what is happening. Much luck!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-29-2009, 03:47 AM
SwEeTAcTioN's Avatar
Member
 
Join Date: Oct 2009
Posts: 69
Rep Power: 0
SwEeTAcTioN is on a distinguished road
Default
What do you mean?
__________________
Are you suggesting that Cocunuts migrate?!! -Monty Python
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-29-2009, 03:56 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,461
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by SwEeTAcTioN View Post
What do you mean?
You've written a program with many if / else blocks that use non-standard indentation. Since we more easily understand that which is familiar to us, if you posted the code with standard indentation, more folks would more easily understand your code at a glance, and be more willing to read it and help you that's all.

You can find out more about Java standard coding conventions here: Coding Conventions Doc
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-30-2009, 04:09 AM
SwEeTAcTioN's Avatar
Member
 
Join Date: Oct 2009
Posts: 69
Rep Power: 0
SwEeTAcTioN is on a distinguished road
Default
Thanks i read it and it makes sense and i also fixed my code
__________________
Are you suggesting that Cocunuts migrate?!! -Monty Python
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
while-loop stopping on first loop davester New To Java 6 06-26-2009 08:46 PM
Need help with a loop ReV13 New To Java 15 12-01-2008 04:48 PM
Need help with While Loop mrdestroy New To Java 14 10-20-2008 02:29 PM
How to use While loop Java Tip java.lang 0 04-17-2008 07:44 PM
can you help me with this for loop? java_fun2007 New To Java 6 12-22-2007 10:20 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:20 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org