# guessing game

Page 1 of 1

## 6 Replies - 1247 Views - Last Post: 09 October 2012 - 08:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294990&amp;s=6a05d3c9cc267e84ce35eabc3e8dc26f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tjarmon52

Reputation: -2
• Posts: 4
• Joined: 09-October 12

# guessing game

Posted 09 October 2012 - 08:07 PM

I have to make a guessing game using java this is what i have so far. Im having a hard time trying to figure out how to keep track of the numbers so i can compare them. For example if the random number is 62 and the user enters 34 it needs to say too low but if the user then enters 32 after 34 it needs to say something like if 34 is too low 34 is also too low. And im also need help when i print out do you want to play again (y/n) i dont know how to make it play the game again. Please help!!!!

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

int round=0;
while(round>=0){
round++;

while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;

if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();

if (x.equalsIgnoreCase(yes)){
//make the program repeat
}

if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}

}
}
}

Is This A Good Question/Topic? 0

## Replies To: guessing game

### #2 Zoquo

Reputation: 20
• Posts: 45
• Joined: 16-May 12

## Re: guessing game

Posted 09 October 2012 - 08:14 PM

First step: put your code in code tags.

This post has been edited by Zoquo: 09 October 2012 - 08:15 PM

### #3 tjarmon52

Reputation: -2
• Posts: 4
• Joined: 09-October 12

## Re: guessing game

Posted 09 October 2012 - 08:28 PM

Zoquo, on 09 October 2012 - 08:14 PM, said:

First step: put your code in code tags.

I have to make a guessing game using java this is what i have so far. Im having a hard time trying to figure out how to keep track of the numbers so i can compare them. For example if the random number is 62 and the user enters 34 it needs to say too low but if the user then enters 32 after 34 it needs to say something like if 34 is too low 34 is also too low. And im also need help when i print out do you want to play again (y/n) i dont know how to make it play the game again. Please help!!!!

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

[code]
int round=0;
while(round>=0){
round++;
[code/]

[code]
while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;
[code/]

[code]
if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
[code/]

[code]
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();
[code/]

[code]
if (x.equalsIgnoreCase(yes)){
//make the program repeat
}

if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}[code/]

}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

int round=0;
while(round>=0){
round++;

while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;

if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();

[code]
if (x.equalsIgnoreCase(yes)){
//make the program repeat
}
[code/]

if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}

}
}
}

### #4 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: guessing game

Posted 09 October 2012 - 08:34 PM

Sigh...Step by step guide here we go.
Tell us EXACTLY where you need help.
Copy/paste code. Highlight code. Press the code image icon above this text area.
Then use preview to make sure everything looks right.
Press submit.

### #5 Zoquo

Reputation: 20
• Posts: 45
• Joined: 16-May 12

## Re: guessing game

Posted 09 October 2012 - 08:34 PM

Second step: put your code in code tags.

### #6 tjarmon52

Reputation: -2
• Posts: 4
• Joined: 09-October 12

## Re: guessing game

Posted 09 October 2012 - 08:47 PM

I have to make a guessing game using java this is what i have so far. Im having a hard time trying to figure out how to keep track of the numbers so i can compare them. For example if the random number is 62 and the user enters 34 it needs to say too low but if the user then enters 32 after 34 it needs to say something like if 34 is too low 34 is also too low. And im also need help when i print out do you want to play again (y/n) i dont know how to make it play the game again. Please help!!!!

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

int round=0;
while(round>=0){
round++;

while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;

if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();

[code]
Here i need to make the guessing game repeat if the user enters a y
if (x.equalsIgnoreCase(yes)){
//make the program repeat
}

[code/]

if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}

}
}
}

### #7 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: guessing game

Posted 09 October 2012 - 08:52 PM

Go to full editor. Use preview before you post anything. If your code is not in code tags. Do not bother posting. If you decide to keep trolling, we can all just wait for the admins. If you actually want help I suggest you put a bit of a effort besides copying and pasting the same post over and over..