Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5
Thread: help with a simple java problem

02172011, 12:40 PM #1
 Join Date
 Dec 2010
 Posts
 10
 Thanks
 2
 Thanked 0 Times in 0 Posts
help with a simple java problem
Hello, so, as the title says, I could use some help with a simple problem I've run into when trying to code a little game, to try and refresh my java skills (which clearly are lacking severely >.< ).
So, I made a game and I want to have the players take turns. I've decided to try and challenge myself and make the turns be decided on who can guess nearest to a random number from 0100.
I have the random number generator and the user inputs, but I'm running into trouble figuring out how to find out who got nearest to the generated number.
I have the two variables, p1_guess and p2_guess, right now and the random number generator.
If anyone can offer any help or advice on this it would be most appreciated, and don't worry, it's not a homework assignment; I'm a sad person and do this for "fun"... XD
Thank you,
Bananas.
02172011, 01:17 PM
#2
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 16,987
 Thanks
 4
 Thanked 2,660 Times in 2,629 Posts
What's the problem exactly? Without any code here its difficult to diagnose what you are even doing.
If you just want to compare the differences you need two more variables, one to track the difference between p1_guess and the random variable, and the other between p2_guess and random number. Whichever is smaller is closer.
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');
02172011, 01:37 PM
#3
 Join Date
 Dec 2010
 Posts
 10
 Thanks
 2
 Thanked 0 Times in 0 Posts
Sorry about the non descriptive title and the lack of code, I'll try to explain what I'm having trouble with in this post ^.^
This is what I have so far to fix the problem:
I know I have to subtract the two guesses against the generator and then compare the values, but I heard about a little line of code in the Math class called Math.abs() which will give me the absolute value of the result, but unfortunately, I am unaware of how to use this piece of code...Code:int p1_guess; int p2_guess; int maybe_p1; int maybe_p2; Scanner input = new Scanner (System.in); InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader stdin = new BufferedReader (inStream); Random generator = new Random(100); System.out.println("The random generator has generated a number, guess what it was"); p1_guess = input.nextInt(); p2_guess = input.nextInt(); maybe_p1 = generator  p1_guess; maybe_p2 = generator  p2_guess;
Also, because my Java skills are terrible, I was unaware that you couldn't subtract integer variables from a random number generator, so when I got to the line:
I got this error:Code:maybe_p1 = generator  p1_guess;
I'm not sure how to fix that...Code:operator  cannot be applied to java.util.Random,int maybe_p1 = generator  p1_guess;
So yeah, any help would be very nice,
Bananas.
02172011, 02:50 PM
#4
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 16,987
 Thanks
 4
 Thanked 2,660 Times in 2,629 Posts
Generator is an object, you cannot apply an operator on it (and Java does not allow overloading of operators). Wrappers do allow operators to act on them.
All you can do is extract from it usingint iRand = Random.nextInt();
. Note however that your results will be unexpected; the parameter in Random() is its seed, not a maximum value. My guess is that 100 is supposed to represent what the maximum possible number is. What you will likely want is:
That will give you a random number between 0 and 100. If you want between 1 and 100:PHP Code:
Random r = new Random();
int iRandom = r.nextInt(101); // r.nextInt's parameter is exclusive
Absolute is a simple static call:PHP Code:
int iRandom = r.nextInt(100) + 1;
That should assign iNumber the value of 356. Note as well that this can just as easily be done without the use of Math.abs by using branching (I'll use the ternary since its shorter):PHP Code:
int iNumber = 356;
iNumber = Math.abs(iNumber);
PHP Code:
iNumber = (iNumber < 0) ? (iNumber * 1) : iNumber;
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');
Users who have thanked FouLu for this post:
dabananaman (02172011)
02172011, 03:06 PM
#5
 Join Date
 Dec 2010
 Posts
 10
 Thanks
 2
 Thanked 0 Times in 0 Posts
Thank you very much, you just saved me a ton of headaches ^.^