Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Evaluating two ints at the same time

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Evaluating two ints at the same time

    What I'm doing is running two different random objects to get two random ints, and then doing an if-else statement to run a different section of code depending on which two random ints are selected. If I set it so that it selects just one random int and then runs an if-else statement based on which single int is selected, it works fine. But I can't get it to evaluate two ints at once. When I try to do this, the code in the if-else statements doesn't run at all.

    Here's the code...

    Random rnd2 = new Random();
    int vehiclecolour = rnd2.nextInt(14);
    System.out.println("Vehicle colour: " + vehiclecolour);

    Random rnd3 = new Random();
    int vehicletype = rnd3.nextInt(6);
    System.out.println("Vehicle type: " + vehicletype);


    if (vehiclecolour==1 && vehicletype==1)
    {

    //Run some code
    }

    else if (vehiclecolour==1 && vehicletype==2)
    {

    //Run some code
    }

    else if (vehiclecolour==1 && vehicletype==3)
    {

    //Run some code
    }


    This continues for all of the remaining possible combinations of the two random ints. Like I said, if I change the code so that only one random int is evaluated, then it will work fine, but when I run it as above, the code inside the if-else statements doesn't run at all. So what am I doing wrong here?

    Also, as a side question - is there any way to get it so that the random int objects won't select 0?

    Thanks!
    Last edited by Sean448; April 22nd, 2012 at 04:18 AM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Evaluating two ints at the same time

    Hello Sean448!
    Quote Originally Posted by Sean448 View Post
    What I'm doing is running two different random objects to get two random ints, and then doing an if-else statement to run a different section of code depending on which two random ints are selected. If I set it so that it selects just one random int and then runs an if-else statement based on which single int is selected, it works fine. But I can't get it to evaluate two ints at once. When I try to do this, the code in the if-else statements doesn't run at all.

    This continues for all of the remaining possible combinations of the two random ints. Like I said, if I change the code so that only one random int is evaluated, then it will work fine, but when I run it as above, the code inside the if-else statements doesn't run at all. So what am I doing wrong here?
    If I were you, i would make the random objects return two values(ie 1 and 2), change the if statements to evaluate these four combinations (1-1, 1-2, 2-1, 2-2). If that worked, I would then expand it. This way you will be able to find the bug easier.

    Quote Originally Posted by Sean448 View Post
    Also, as a side question - is there any way to get it so that the random int objects won't select 0?
    The nextInt(int n) method returns an int between 0 (inclusive) and n (exclusive).
    Therefore you cannot make it not select 0. But you can add 1 to the value it returns. For example, if you want a random int between 1 and 6 you can have the following statement:
    int x = 1 + random.nextInt(6);

    Hope it helps.

Similar Threads

  1. [SOLVED] evaluating postfix expressions using stack
    By lieles in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2011, 11:48 AM
  2. Evaluating Expressions
    By javapenguin in forum What's Wrong With My Code?
    Replies: 19
    Last Post: November 16th, 2010, 08:30 PM
  3. VM re-evaluating a string
    By Johannes in forum Java Theory & Questions
    Replies: 6
    Last Post: September 20th, 2010, 04:44 PM
  4. why is this statement evaluating false??
    By humdinger in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2009, 04:28 PM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM