Java Help!

QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
<div class="IPBDescription">D:</div> I'm learning to code java, and I'm doing well, but there are a few things I've been wondering:

How do I create a random number, and after that, put paramaters on it(like 1-100, 1-10, all that).

I'll have more questions later, but this is the most pressing.
«1

Comments

  • NEO_PhyteNEO_Phyte We need shirtgons&#33; Join Date: 2003-12-16 Member: 24453Members, Constellation
    edited February 2005
    /me loads up his java applets


    RandomIntGenerator( <lowestnumber> , <highestnumber> )

    the numbers CAN be negative
  • CabooseCaboose title = name(self, handle) Join Date: 2003-02-15 Member: 13597Members, Constellation
    public int Random.nextInt(int 10)
    {
    //stuff between 0 and 10
    }

    I think, I'm still a Java nublet myself
  • RPG_JssmfulhudRPG_Jssmfulhud Join Date: 2002-11-02 Member: 4006Members
    Math.round(Math.random()*100);
  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    Straght from the Java API, all the official info you could ever need on <a href='http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(int)' target='_blank'>public int nextInt(int n) of the Random class</a>
  • NiteowlNiteowl Join Date: 2002-09-04 Member: 1274Members, NS1 Playtester, Contributor
    edited February 2005
    mmmmm, Javaaaaaa...

    IIRC, RPG Jssmfulhud answer wins.


    edit: oh ignore me, what do I know, Marik's is obviously correct
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->    
    Random r = new Random();
    int randomInt = r.nextInt();
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • Mad_ManMad_Man Join Date: 2003-06-13 Member: 17359Members, Constellation
    Indeed it is <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Random r = new Random();
    int randomInt = r.nextInt();<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    And you should know that if your plaining on taking the AP cs test.
  • LeetVegeTaLeetVegeTa Join Date: 2003-02-27 Member: 14103Members
    just to add to that, you can also do this:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Random r = new Random();
    int randomInt = r.nextInt(n);<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    for a random number between 0 and n-1

    or
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Random r = new Random();
    int randomInt = r.nextInt(n) + 1;<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    for a random number between 1 and n
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    edited February 2005
    <!--QuoteBegin-Mad Man+Feb 8 2005, 02:37 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Mad Man @ Feb 8 2005, 02:37 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Indeed it is <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->Random r = new Random();
    int randomInt = r.nextInt();<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
    And you should know that if your plaining on taking the AP cs test. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Thats next year! This year is just wierd...stuff. Yeah. Hard to explain.

    Thanks folks for this. I'm wanting to make my own little mini text RPG as a way of showing how even in basic programming, that unique game designs can come out(being all my friends are like "You must remake a classic like Pac Man or pong!")

    <3

    Edit: And I *hate* the Java API. It doesn't show you anything, doesn't explain anything in terms that anyone can understand- its like a reference for people who've already memorized all of java's commands! <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad-fix.gif' border='0' style='vertical-align:middle' alt='sad-fix.gif' /><!--endemo-->
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    It's pretty useful when you can't remember how to (for example) get an object's dimensions, and just need to look up the method. It's saved my butt a couple of times.
  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    <!--QuoteBegin-Quaunaut+Feb 8 2005, 06:03 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Quaunaut @ Feb 8 2005, 06:03 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> [...]
    Edit: And I *hate* the Java API. It doesn't show you anything, doesn't explain anything in terms that anyone can understand- its like a reference for people who've already memorized all of java's commands! <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad-fix.gif' border='0' style='vertical-align:middle' alt='sad-fix.gif' /><!--endemo--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    To quote George Orwell, "The best books are the ones that tell you what you already know." <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->

    True, finding out specifics about stuff requires that you have between a general and good idea of what you're looking for...but when you do, the API helps. For instance, it's very common to forget whether nextInt(6) will generate numbers from 0 to 6, 1 to 6, 0 to 5, 1 to 5....the api lets you know exactly.
  • Mad_ManMad_Man Join Date: 2003-06-13 Member: 17359Members, Constellation
    I never look at the java api doc really, but it does annoy me in java with lack of conisitency. Such as .size() .length .length() and I think theres one or two more
  • TychoCelchuuuTychoCelchuuu Anememone Join Date: 2002-03-23 Member: 345Members
    I'm a little fuzzy on all this but I seem to recall the common way to set parameters on the random number would be to use the modulus operand to do something or other.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Quick word of advice: mini text RPGs can have a nasty habit of taking WAY longer than expected to finish. I started writing one when I was learning QBasic expecting it to take a handful of months, and it ended up dragging out over several years (and still isn't finished).
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-SoulSkorpion+Feb 8 2005, 07:31 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Feb 8 2005, 07:31 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Quick word of advice: mini text RPGs can have a nasty habit of taking WAY longer than expected to finish. I started writing one when I was learning QBasic expecting it to take a handful of months, and it ended up dragging out over several years (and still isn't finished). <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Well, I actually want it to be rather simple. First I wanna dev my own battle system and a level up system. From there, I'll construct a first mini-story so that I can construct a stats based interaction system, similar to that in Deus Ex, or Bloodlines(being its text based, it should be merely a matter of creating the choice when a certain stat is reached).

    I know it'll be lots of work, but it'll help me learn! <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin-fix.gif' border='0' style='vertical-align:middle' alt='biggrin-fix.gif' /><!--endemo-->
  • Mad_ManMad_Man Join Date: 2003-06-13 Member: 17359Members, Constellation
    The only thing I could imigine that would take a while in a text rpg is making all the monsters and typing all the stuff out. But if you used a text file to store info like monster stats and player saves I couldnt see it taking more then 2 months
  • NiteowlNiteowl Join Date: 2002-09-04 Member: 1274Members, NS1 Playtester, Contributor
    <!--QuoteBegin-Quaunaut+Feb 8 2005, 03:03 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Quaunaut @ Feb 8 2005, 03:03 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Edit: And I *hate* the Java API. It doesn't show you anything, doesn't explain anything in terms that anyone can understand- its like a reference for people who've already memorized all of java's commands! <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad-fix.gif' border='0' style='vertical-align:middle' alt='sad-fix.gif' /><!--endemo--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    <a href='http://java.sun.com/docs/books/tutorial/' target='_blank'>Excellent resource, every link <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo--></a>

    *waves to qua*
    hullo you voicespamming monkey you!
  • SwiftspearSwiftspear Custim tital Join Date: 2003-10-29 Member: 22097Members
    edited February 2005
    <!--QuoteBegin-SoulSkorpion+Feb 8 2005, 09:31 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Feb 8 2005, 09:31 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Quick word of advice: mini text RPGs can have a nasty habit of taking WAY longer than expected to finish. I started writing one when I was learning QBasic expecting it to take a handful of months, and it ended up dragging out over several years (and still isn't finished). <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Qbasic is a terrible language for writing text RPG's though, the things get whipped off like no time in an object oriented language. The sequential line number hopping system of Qbasic is just not condusive to statistics storage or randomly generated events, and if you try to actually include a story, good luck because you are screwed.

    [edit] this whole topic makes me want to relearn visual C++ and start coding out some of my text RPG ideas...
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin-Swiftspear+Feb 9 2005, 03:16 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Swiftspear @ Feb 9 2005, 03:16 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-SoulSkorpion+Feb 8 2005, 09:31 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Feb 8 2005, 09:31 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Quick word of advice: mini text RPGs can have a nasty habit of taking WAY longer than expected to finish. I started writing one when I was learning QBasic expecting it to take a handful of months, and it ended up dragging out over several years (and still isn't finished). <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Qbasic is a terrible language for writing text RPG's though, the things get whipped off like no time in an object oriented language. The sequential line number hopping system of Qbasic is just not condusive to statistics storage or randomly generated events, and if you try to actually include a story, good luck because you are screwed.

    [edit] this whole topic makes me want to relearn visual C++ and start coding out some of my text RPG ideas... <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Oh, it worked fine. It just went through several design iterations, took on mass, and eventually grew into a kind of single player MUD complete with a custom scripting language. A hell of a lot of fun to write, it just never got finished before I decided I'd better hurry up and move on to C.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    Oh! I remembered my other question.

    See, my instructor is teaching us things according to a book, and when it comes down to it, he's barely 3 days ahead of us. Heh. But I was wondering, what command could make me go to whatever line I tell it to? A'la the "Goto" command(if I am thinking of it correctly). Thanks <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin-fix.gif' border='0' style='vertical-align:middle' alt='biggrin-fix.gif' /><!--endemo-->
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    Could you give a bit of context? You can usually get by with other methods.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    Say...

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->import javax.swing.JOptionPane;
    class player {
    static int strength;
    static int dexterity;
    static int agility;
    static int intelligence;
    static int wisdom;
    static int charisma;
    static int hp;
    static int mp;
    static String name;
    }

    class monster {
    }

    class rpg {
    public static void main(String args[]) {
    String Choicepoints;
     player.name = JOptionPane.showInputDialog("What is your character's name?");
     System.out.println("Player name is "+player.name);
     player.strength = 0;
     player.dexterity=0;
     player.agility=0;
     player.intelligence=0;
     player.wisdom=0;
     player.charisma=0;
     
     int x = RandomIntGenerator(1,10);

    System.out.println(x);

     for(int choicepoints=30;choicepoints>0;choicepoints--){
      String Choices;
      int choices;
      Choices = JOptionPane.showInputDialog("Choose what to put your attribute points into?"+
      "\n 1. Strength : "+player.strength+
      "\n 2. Dexterity : "+player.dexterity+
      "\n 3. Agility : "+player.agility+
      "\n 4. Intelligence : "+player.intelligence+
      "\n 5. Wisdom : "+player.wisdom+
      "\n 6. Charisma : "+player.charisma+
      "\n Choose by number. You have "+choicepoints+" points left to put into attributes."+
      "\n Just remember to put at least a few points into everything, or you may find your character...limited.");
      choices = Integer.parseInt(Choices);
      if(choices==1)player.strength++;
      if(choices==2)player.dexterity++;
      if(choices==3)player.agility++;
      if(choices==4)player.intelligence++;
      if(choices==5)player.wisdom++;
      if(choices==6)player.charisma++;
     
     
    }
    player.hp=(player.strength*5)+20;
    player.mp=(player.intelligence*5)+10;
     
    System.out.println(player.name+"'s Character Sheet:");
    System.out.println();
    System.out.println("Health: "+player.hp);
    System.out.println("Mana: "+player.mp);
    System.out.println();
    System.out.println("Strength: "+player.strength);
    System.out.println("Dexterity: "+player.dexterity);
    System.out.println("Agility: "+player.agility);
    System.out.println("Intelligence: "+player.intelligence);
    System.out.println("Wisdom: "+player.wisdom);
    System.out.println("Charisma: "+player.charisma);

           for(;player.hp=0;;) {
               //the battle system

              if(monster.hp=0)[B]Goto command goes here. It'll go to the line above this very for loop.[/B]
           }<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    Yeah, at that ending part. And yes, I realize that my current thing of this RPG is pretty basic- I'm still working on learning how to manipulate the JOptionPane stuff, as well as frame modifiers.

    Also, being I haven't been able to use it yet until now, I can't seem to figure out how to get the random thing to work. Could someone show me it in a working context?
  • ThansalThansal The New Scum Join Date: 2002-08-22 Member: 1215Members, Constellation
    edited February 2005
    goto = bad

    just make an operation (aka monsterDed) that gets called when monster.hp = 0

    forinstance you could have an action that accepts a monster type object for its argument.

    so you call monsterDed(*insert monster you are playing with atm here*) and it will pull out how much xp, what loot etc etc should be added to your character <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->

    and you can ofcourse create a class that handels all combat by simply passing it a playerChar object and a monster object.


    hehe, I havn't messed around with Java in a whiel, but I still like it (I also definatly recomend that you get ussed to ussing the API as you will often find a simple way to do omething you have been doing the long way all along)

    I had a project where I had to encode a number. We were suposed to use for loops, I found a piece in the API that let me simply set a deffenition for what each number should be changed into <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo--> (oh, yah the grader told me not do do it b/c we were technicaly suposed to be doing for loops, so I left it in my code commented out <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->)
  • Nemesis_ZeroNemesis_Zero Old European Join Date: 2002-01-25 Member: 75Members, Retired Developer, NS1 Playtester, Constellation
    I'd just like to say - you all are talking gibberish.
  • QuaunautQuaunaut The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
    <!--QuoteBegin-Nemesis Zero+Feb 9 2005, 10:24 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Nemesis Zero @ Feb 9 2005, 10:24 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I'd just like to say - you all are talking gibberish. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    You hate Java, don't you?
  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    <!--QuoteBegin-Quaunaut+Feb 9 2005, 12:28 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Quaunaut @ Feb 9 2005, 12:28 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-Nemesis Zero+Feb 9 2005, 10:24 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Nemesis Zero @ Feb 9 2005, 10:24 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I'd just like to say - you all are talking gibberish. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    You hate Java, don't you? <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Nah, he makes comments like that in the Super Secret forums whenever we start talking about Linux or other coding-related stuff, too <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
  • ThansalThansal The New Scum Join Date: 2002-08-22 Member: 1215Members, Constellation
    Nem0 is a humaneties person aint he? (psychology?)


    pshaw (please ignore the fact that I was gona get a psyche major)
  • antichristantichrist Join Date: 2003-05-27 Member: 16769Members
    C# is your friend <!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html/emoticons/nerd-fix.gif' border='0' style='vertical-align:middle' alt='nerd-fix.gif' /><!--endemo-->
  • SwiftspearSwiftspear Custim tital Join Date: 2003-10-29 Member: 22097Members
    edited February 2005
    <!--QuoteBegin-Quaunaut+Feb 9 2005, 10:31 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Quaunaut @ Feb 9 2005, 10:31 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Oh! I remembered my other question.

    See, my instructor is teaching us things according to a book, and when it comes down to it, he's barely 3 days ahead of us. Heh. But I was wondering, what command could make me go to whatever line I tell it to? A'la the "Goto" command(if I am thinking of it correctly). Thanks <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin-fix.gif' border='0' style='vertical-align:middle' alt='biggrin-fix.gif' /><!--endemo--> <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    I don't know really any java, but I was under the impression that it handled this alot like C++... basicly there is no goto command, what you would do with goto loops in QB is done massively more efficiantly with loop commands, function calls, and multiple objects. If you need to back track your story in the game it should be built as multiple objects so that you can call them at any point in time from any point in the game. and levels and statistics should not under any circumstance be stored anywhere but in objects or callable functions.

    [edit]oh, and Soulscorpion, I wasn't saying that Qbasic isn't capable of building text RPG's, I have a few partially finished ones in it myself. I was just saying that Qbasic is much messier and no where near as efficiant for building them as any of the object oriented languages are.
  • Nemesis_ZeroNemesis_Zero Old European Join Date: 2002-01-25 Member: 75Members, Retired Developer, NS1 Playtester, Constellation
    <!--QuoteBegin-Thansal+Feb 9 2005, 06:23 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Thansal @ Feb 9 2005, 06:23 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Nem0 is a humaneties person aint he? (psychology?) <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Actually, psychology is, like my own media sciences, a social, not a humanistic science. We're bound to empirical data, they're not <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->


    But yeah, whenever people start talking about advanced technicals, part of my brain just shuts down.
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    <!--QuoteBegin-antichrist+Feb 9 2005, 04:54 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (antichrist @ Feb 9 2005, 04:54 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> C# is your friend <!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html/emoticons/nerd-fix.gif' border='0' style='vertical-align:middle' alt='nerd-fix.gif' /><!--endemo--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Your name is oddly fitting
Sign In or Register to comment.