
10-28-2009, 04:37 AM
|
 |
Member
|
|
Join Date: Oct 2009
Posts: 69
Rep Power: 0
|
|
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
|
|

10-28-2009, 05:07 AM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 3
Rep Power: 0
|
|
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!");
}
}
}
}
} |
|
|

10-29-2009, 03:19 AM
|
 |
Member
|
|
Join Date: Oct 2009
Posts: 69
Rep Power: 0
|
|
|
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
|
|

10-29-2009, 03:35 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 8,461
Rep Power: 11
|
|
|
You may get more help if you format your code better. As displayed, it's difficult to see what is happening. Much luck!
|
|

10-29-2009, 03:47 AM
|
 |
Member
|
|
Join Date: Oct 2009
Posts: 69
Rep Power: 0
|
|
|
What do you mean?
__________________
Are you suggesting that Cocunuts migrate?!! -Monty Python
|
|

10-29-2009, 03:56 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 8,461
Rep Power: 11
|
|
Originally Posted by SwEeTAcTioN
|
|
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
|
|

10-30-2009, 04:09 AM
|
 |
Member
|
|
Join Date: Oct 2009
Posts: 69
Rep Power: 0
|
|
|
Thanks i read it and it makes sense and i also fixed my code
__________________
Are you suggesting that Cocunuts migrate?!! -Monty Python
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
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