Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    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 0-100.

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    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'); 

  • #3
    New Coder
    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:

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


    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:

    Code:
    maybe_p1 = generator - p1_guess;
    I got this error:
    Code:
    operator - cannot be applied to java.util.Random,int
    		maybe_p1 = generator - p1_guess;
    I'm not sure how to fix that...

    So yeah, any help would be very nice,

    Bananas.

  • #4
    God Emperor Fou-Lu's Avatar
    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 using int 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:
    PHP Code:
    Random r = new Random();
    int iRandom r.nextInt(101); // r.nextInt's parameter is exclusive 
    That will give you a random number between 0 and 100. If you want between 1 and 100:
    PHP Code:
    int iRandom r.nextInt(100) + 1
    Absolute is a simple static call:
    PHP Code:
    int iNumber = -356;
    iNumber Math.abs(iNumber); 
    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:
    iNumber = (iNumber 0) ? (iNumber * -1) : iNumber
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    dabananaman (02-17-2011)

  • #5
    New Coder
    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 ^.^


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •