Results 1 to 7 of 7
  1. #1
    Sean2012 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Choose between to id's (or)

    Hello I have this line of code

    Java Code:
    player.getInventory().addItem(1856, 1);
    1856 = item id
    1 = amount

    I would like it to choose between two objects at random. So lets say I have it set up like (1856, 1) (1870, 1) and then it will choose one of those at random.

    I have tried setting it up multiple ways and can't figure it out..

    Thanks for any help!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Choose between to id's (or)

    Quote Originally Posted by Sean2012 View Post
    it will choose one of those at random.

    I have tried setting it up multiple ways and can't figure it out.
    Tried googling "Java random?"

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Sean2012 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Choose between to id's (or)

    Yes i'm checking it out right now. I have set up a class file that looks like this

    Java Code:
    public class PkDrops {
    	
    	public static int PkDrops[] = {3101,4151,11694,11696,11698,11700};
    
    	public static int PkDrops()
    	{
    		return PkDrops[(int)(Math.random()*PkDrops.length)];
    	}
    }
    Does this look correct?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Choose between to id's (or)

    Use the more modern java.util.Random instead. Shortens your code and makes it more readable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Sean2012 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Choose between to id's (or)

    Quote Originally Posted by DarrylBurke View Post
    Use the more modern java.util.Random instead. Shortens your code and makes it more readable.

    db
    How would I do that sorry i'm fairly new to Java.

    Also I'm confused on how to get this to print the selection.

    Java Code:
    package src;
    
    public class PkDrops {
    
    	public static int PkDrop[] = {3101,4151,11694,11696,11698,11700};
    
    	public static int PkDrop()
    	{
    		return PkDrop[(int)(Math.random()*PkDrop.length)];
    	}
    	
    public static void main(String[] args){
    		System.out.println("PkDrop");
    	}
    }
    Last edited by Sean2012; 04-11-2012 at 08:29 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Choose between to id's (or)

    Quote Originally Posted by Sean2012 View Post
    How would I do that sorry i'm fairly new to Java.
    Do you know where to find the API?

    db

    edit Go through http://www.oracle.com/technetwork/ja...oc-136057.html . Variable and method names should start with a lowercase letter.

    And it's poor design to have a variable and a method with the same name. You're only going to confuse yourself and anyone who reads your code with that.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Sean2012 is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Choose between to id's (or)

    Quote Originally Posted by DarrylBurke View Post
    Do you know where to find the API?

    db

    edit Go through Code Conventions for the Java Programming Language: Contents . Variable and method names should start with a lowercase letter.

    And it's poor design to have a variable and a method with the same name. You're only going to confuse yourself and anyone who reads your code with that.

    Okay, Thanks for the help! I figured it out.

Similar Threads

  1. 12 reasons why I choose to be in IT industry
    By DK Bello in forum Entertainment
    Replies: 1
    Last Post: 01-06-2012, 09:53 AM
  2. help to choose
    By NoWayOut in forum New To Java
    Replies: 3
    Last Post: 06-05-2011, 04:54 PM
  3. how to choose the field where last JTextField
    By aledara in forum Advanced Java
    Replies: 8
    Last Post: 12-07-2010, 11:16 PM
  4. what to choose???(awt or swing)
    By moshe12007 in forum AWT / Swing
    Replies: 11
    Last Post: 08-14-2009, 12:10 PM
  5. Replies: 0
    Last Post: 08-27-2008, 10:43 AM

Posting Permissions

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