guessing game problem ?

Page 1 of 1

12 Replies - 1114 Views - Last Post: 14 March 2012 - 08:05 AMRate 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=270788&amp;s=57e033ede28d80c2414ad8d1c9d0a6ed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 stephanie904

Reputation: 1
• Posts: 31
• Joined: 14-February 12

guessing game problem ?

Posted 13 March 2012 - 08:00 PM

I am only having one problem and I dont know how to fix it

when I put a number that is lower than the number I am supposed to get, it tells me "Too high, try again"

how can I fix this so that the message will tell me that the number is lower than what its supposed to be ?

For example :

program gives me number 392

I put 391

and it tells me number is too high, try again

```import java.util.Random;
import javax.swing.JOptionPane;

public class
{
public static void main(String [] args)
{

Random randomNumbers = new Random();
int num = 1 + randomNumbers.nextInt(1000);
System.out.println(num);

String a =
JOptionPane.showInputDialog("Guess a number between 1 and 1000? ");
int b = Integer.parseInt(a);

String message;
if(num > B)/> {
message = "Too high, try again.";
} else if(num < B)/> {
message = "Too low, try again.";
} else if  (num==B)/>{

message = "You're right, the number is" + num ;
message = "You guessed" + "times";
}
else{message= "wrong number";}
JOptionPane.showMessageDialog(null, message);

}

}

```

Is This A Good Question/Topic? 0

Replies To: guessing game problem ?

#2 jon.kiparsky

• Pancakes!

Reputation: 5440
• Posts: 8,756
• Joined: 19-March 11

Re: guessing game problem ?

Posted 13 March 2012 - 08:08 PM

Is this your actual code? Java is case sensitive, so "b" and "B" are different variables. This would make a difference, but this code wouldn't compile(since there isn't a variable B in scope)
It'd also not compile since you need a class name. This here is no good:

```
public class
{

```

There's also a problem here:

```   String message;
if(num > B)/> {
message = "Too high, try again.";
} else if(num < B)/> {
message = "Too low, try again.";
} else if  (num==B)/>{

message = "You're right, the number is" + num ;
message = "You guessed" + "times";
}
else{message= "wrong number";}
JOptionPane.showMessageDialog(null, message);

}
```

The variable message takes on the last value assigned to it, so this
```   message = "You're right, the number is" + num ;
message = "You guessed" + "times";
```

is the same as
```   message = "You guessed" + "times";
```

And of course, there is no way you can get to the last else clause unless num or B change in the course of the decision, since for any two numbers A and B, it must be the case that A==B or A<B or A>B.

#3 pbl

• There is nothing you can't do with a JTable

Reputation: 8032
• Posts: 31,202
• Joined: 06-March 08

Re: guessing game problem ?

Posted 13 March 2012 - 08:45 PM

@Jon: It is the forum browser that converts b ) into B )

Also @stephanie

```if(xxx < 0)
....
else if(xxx > 0)
....
else if(xxx == 0)     // no need to check if xxx == 0 can't be anything else !!
....
else  .....           // can't be anything else already proven

```

This post has been edited by pbl: 13 March 2012 - 08:52 PM

#4 jon.kiparsky

• Pancakes!

Reputation: 5440
• Posts: 8,756
• Joined: 19-March 11

Re: guessing game problem ?

Posted 13 March 2012 - 08:47 PM

pbl, on 13 March 2012 - 10:45 PM, said:

@Jon: It is the forum browser that converts b ) into B )

That's right, I forgot about that. For purposes of smiley faces, I think.
Should do that in code tags, though, I'd call that a bug.

#5 pbl

• There is nothing you can't do with a JTable

Reputation: 8032
• Posts: 31,202
• Joined: 06-March 08

Re: guessing game problem ?

Posted 13 March 2012 - 08:51 PM

jon.kiparsky, on 13 March 2012 - 10:47 PM, said:

For purposes of smiley faces, I think.
Should do that in code tags, though, I'd call that a bug.

I agree. But if users were using meaningful variable names it wouldn't happen

#6 stephanie904

Reputation: 1
• Posts: 31
• Joined: 14-February 12

Re: guessing game problem ?

Posted 13 March 2012 - 09:06 PM

pbl, on 13 March 2012 - 08:45 PM, said:

@Jon: It is the forum browser that converts b ) into B )

Also @stephanie

```if(xxx < 0)
....
else if(xxx > 0)
....
else if(xxx == 0)     // no need to check if xxx == 0 can't be anything else !!
....
else  .....           // can't be anything else already proven

```

thank you ! it worked

#7 pbl

• There is nothing you can't do with a JTable

Reputation: 8032
• Posts: 31,202
• Joined: 06-March 08

Re: guessing game problem ?

Posted 13 March 2012 - 09:08 PM

#8 stephanie904

Reputation: 1
• Posts: 31
• Joined: 14-February 12

Re: guessing game problem ?

Posted 13 March 2012 - 10:13 PM

I actually have one more thing

how can I do it without repeating a new string message that would keep asking number until the users guess the number ???

#9 stephanie904

Reputation: 1
• Posts: 31
• Joined: 14-February 12

Re: guessing game problem ?

Posted 14 March 2012 - 05:44 AM

bump

#10 stephanie904

Reputation: 1
• Posts: 31
• Joined: 14-February 12

Re: guessing game problem ?

Posted 14 March 2012 - 07:42 AM

how can I do a new string message that would keep asking number until the users guess the number

it gives me an error in JOptionPane.showMessageDialog(null, message1);

```import java.util.Random;
import javax.swing.JOptionPane;

public class
{
public static void main(String [] args)
{

Random randomNumbers = new Random();
int num = 1 + randomNumbers.nextInt(1000);
System.out.println(num);

String a =
JOptionPane.showInputDialog("Guess a number between 1 and 1000? ");
int b = Integer.parseInt(a);

int numberguessed = 1;
numberguessed++;

String message;
if(num < B)/> {
message = "Too high, try again.";
} else if(num > B)/> {
message = "Too low, try again.";
} else if (num == B)/>{

message = "You're right, the number is " + num ;

}
else{message= "wrong number";}
JOptionPane.showMessageDialog(null, message);

while (num!=B)/>
{String d =
JOptionPane.showInputDialog("Guess a number between 1 and 1000? ");
int guess1 = Integer.parseInt(d);

String message1;
if(num < guess1) {
message = "Too high, try again.";
} else if(num > guess1) {
message = "Too low, try again.";
} else if (num == guess1){

message = "You're right, the number is " + num ;

}
else{message= "wrong number";}
JOptionPane.showMessageDialog(null, message1);
numberguessed++;
b=guess1;

}

}
}
```

#11 jon.kiparsky

• Pancakes!

Reputation: 5440
• Posts: 8,756
• Joined: 19-March 11

Re: guessing game problem ?

Posted 14 March 2012 - 07:46 AM

Try a while loop.
```while (theyDidntGuessTheRightAnswer)
{
}
```

The details I leave up to you.

#12 stephanie904

Reputation: 1
• Posts: 31
• Joined: 14-February 12

Re: guessing game problem ?

Posted 14 March 2012 - 07:58 AM

jon.kiparsky, on 14 March 2012 - 07:46 AM, said:

Try a while loop.
```while (theyDidntGuessTheRightAnswer)
{
}
```

The details I leave up to you.

So i dont need to put a string message you are saying ?

#13 jon.kiparsky

• Pancakes!

Reputation: 5440
• Posts: 8,756
• Joined: 19-March 11

Re: guessing game problem ?

Posted 14 March 2012 - 08:05 AM

I'm saying that a while loop is the best way to keep asking until they get the right answer or get bored and quit. If you need help, it's described here.

What you do with that loop is up to you. It might involve presenting a new String to the user, but it's up to you to work it out. You're a programmer, what you do is solve problems. This is a problem. Solve it.