12 Replies - 1114 Views - Last Post: 14 March 2012 - 08:05 AM Rate Topic: -----

#1 stephanie904  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5440
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8032
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

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

Re: guessing game problem ?

Posted 13 March 2012 - 08:47 PM

View Postpbl, 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.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: guessing game problem ?

Posted 13 March 2012 - 08:51 PM

View Postjon.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 :)
Was This Post Helpful? 1
  • +
  • -

#6 stephanie904  Icon User is offline

  • New D.I.C Head

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

Re: guessing game problem ?

Posted 13 March 2012 - 09:06 PM

View Postpbl, 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 :)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: guessing game problem ?

Posted 13 March 2012 - 09:08 PM

:) :^:
Was This Post Helpful? 0
  • +
  • -

#8 stephanie904  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 ???
Was This Post Helpful? 0
  • +
  • -

#9 stephanie904  Icon User is offline

  • New D.I.C Head

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

Re: guessing game problem ?

Posted 14 March 2012 - 05:44 AM

bump
Was This Post Helpful? 0
  • +
  • -

#10 stephanie904  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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);

help please ?

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;
	
	
}

}
}

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

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

Re: guessing game problem ?

Posted 14 March 2012 - 07:46 AM

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


The details I leave up to you.
Was This Post Helpful? 0
  • +
  • -

#12 stephanie904  Icon User is offline

  • New D.I.C Head

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

Re: guessing game problem ?

Posted 14 March 2012 - 07:58 AM

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

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


The details I leave up to you.


So i dont need to put a string message you are saying ?
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5440
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1