Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
02-19-2011, 08:14 AM #1
- Join Date
- Dec 2010
- Thanked 0 Times in 0 Posts
Using a random generator to make an in game chest in Java
So, I'm making a little game in Java for 'fun'. I want to make it so the two players can guess a number 0-100, and whoever gets closest to the random number generated wins the chest.
So far I have....
Random r = new Random(); int chest = r.nextInt(101); System.out.println("Player 1, guess the random number (from 0 - 100)"); p1_guess = input.nextInt(); System.out.println("Player 2, guess the random number (from 0 - 100)"); p2_guess = input.nextInt(); int p1_answer = p1_guess - chest; //subtracts player 1's guess from the random number int p2_answer = p2_guess - chest; p1_answer = Math.abs(p1_answer); //finds the absolute value p2_answer = Math.abs(p2_answer);
I guess I need to compare the p2_answer and P1_answer and see what is smallest? I'm not really sure how to do that or if that's even what I have to do.
Any help would be very, very helpful.