Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 8 of 8

Thread: Java - Multiple Values For Int

  1. #1

    Java - Multiple Values For Int

    Is it possible?

    Example:
    int x = 100, 200, 300;

    or

    int x =100 || 200 || 300

  2. #2
    No, it isn't possible. What are you trying to do?

    It's possible to have an array of integers:
    Code:
    int nums[] = {1,2,3};
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

  3. #3
    Quote Originally Posted by Krilnon View Post
    No, it isn't possible. What are you trying to do?

    It's possible to have an array of integers:
    Code:
    int nums[] = {1,2,3};
    I need to make int x equal to three different numbers. And my function must always consider each of those three numbers at the same time.

    I need to say this:

    x = 100 || 200 || 300
    if(y > x) {dothis}
    else if(y < x) {dothis}

    where y will always be a random number. No, I can't just do x = 100 and y > x. It's important that x equal three numbers.

  4. #4
    x can't equal three numbers, think of it this way:
    If x = 100 and x = 200, then 100 = 200 which is a contradiction.. therefore x cannot equal 100 and 200.

    What is it that you're trying to do?

  5. #5
    Try either an array or just three different variables:

    x = 100
    z = 200
    a = 300
    if (y > x || y > z || y > a) {do this}
    else {do this}

    You can do the same by going through an array with a for loop, but if you only have three ints that's a little much.

    The question is whether one of the conditions returning true is sufficient, or if all three conditions must be true. That will change the code a little.

  6. #6
    Quote Originally Posted by KillCodyDead View Post
    I need to make int x equal to three different numbers. And my function must always consider each of those three numbers at the same time.

    I need to say this:

    x = 100 || 200 || 300
    if(y > x) {dothis}
    else if(y < x) {dothis}

    where y will always be a random number. No, I can't just do x = 100 and y > x. It's important that x equal three numbers.
    Your best bet ( like everyone else has mentioned) is to create an array. That is the only way I can think of to compare 1 type with 3 values.

    int numbs[] = {100,200,300};

    if (y > numbs[0] || y > numbs[1] || y > numbs[2]){
    // do this code
    }
    ..... etc

    or you can do what battie said and create 3 individual ints.

    Whats the purpose of having 3 values in one int?

  7. #7
    Well, if y is greater than the smallest integer, then technically, doesn't it encompass all of the possible x values?
    got pwnt?

  8. #8
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    I think your best bet is to put each of the values you want x to take into an array as x and then loop over each of these values and call your function with that value (assuming you don't need to use more than one value at a time - in which case your loop should be within your function and it should take an array as it's parameter).

    Most programming languages don't work with vectors - perhaps you're used to working with maple or some other maths package but unfortunately you'll need to work with loops now instead.

    Code:
    void test (x) {
       if (x > 100) {
          // ...
       } else if (x < 50) {
          // ...
       }
    }
    
    int i;
    int x[] = {10,20,30,40,50,60,70,80,90,100};
    
    for (i=0; i<x.length; i++) {
       test(x[i]);
    }
    Note that my java sucks.
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012