09-20-2011, 11:20 PM | #1 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Java help
anyone here that knows java and wants to help me?
There is this program I have to make a random dice generator and I have to have one die object but let it generate 3 numbers anyone know how to do it? Code:
class DieRoll { public static void main ( String [] args) { String space; space = " "; Die die; die = new Die(); die.roll(); System.out.println("Your Results are" + space + die.getRoll()); } } Code:
import java.util.*; class Die { private int number; private static final int MAX_NUMBER = 6; private static final int MIN_NUMBER = 1; private static final int NO_NUMBER = 0; //Constructor public Die() { number = NO_NUMBER; } //Roll a Die public void roll() { number = (int) Math.floor((Math.random() * (MAX_NUMBER - MIN_NUMBER +1) +1)); } // Get number from roll public int getRoll() { return number; } } I guess I can offer you credits Last edited by FFRGreen; 09-20-2011 at 11:29 PM.. |
09-20-2011, 11:24 PM | #2 |
HAVE ME WITH VODKA
Join Date: Aug 2005
Location: Long Island, New York
Age: 35
Posts: 7,316
|
Re: Java help
I took two semesters of it back in college. Not sure if I remember enough to help you. Why not ask the problem in your post so others can respond in the thread to it?
|
09-20-2011, 11:25 PM | #3 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
|
09-20-2011, 11:28 PM | #4 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
|
Re: Java help
So post it.
__________________
|
09-20-2011, 11:34 PM | #5 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
|
09-20-2011, 11:34 PM | #6 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
|
Re: Java help
Well I'll tell you one thing you need.
Code:
import java.util.Random; public int roll() { return generator.nextInt(6) + 1; } If you aren't required to be object oriented, then there would be no need to create a die class at all. Just add a function in the DieRoll class that does the same thing, and call that. ---------------------------------- Never mind you can't do that, deleted it. You need to make a loop. Create a string to store your answers in. Loop: 3 times die.roll(); string += die.getRoll() + " "; Simplified way of doing it.
__________________
Last edited by Zageron; 09-20-2011 at 11:43 PM.. |
09-20-2011, 11:50 PM | #7 | |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
Quote:
|
|
09-20-2011, 11:55 PM | #8 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,981
|
Re: Java help
he means string += die.getRoll() + " " lol
__________________
|
09-21-2011, 12:06 AM | #9 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
|
09-21-2011, 12:15 AM | #10 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
|
Re: Java help
String result = "";
Would create a string to store your answer in. Then in a loop, you roll the die, and then acquire the roll result. The roll result is then appended on to the string. die.getRoll() will be a number, so a toString method will likely be required. Google is your friend! The java documentation never hurt either.
__________________
|
09-21-2011, 12:16 AM | #11 | |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
Quote:
|
|
09-21-2011, 12:25 AM | #12 |
FFR Simfile Author
|
Re: Java help
You haven't done strings yet or you haven't done loops yet? You can just google about both anyway
__________________
Patashu makes Chiptunes in Famitracker: http://soundcloud.com/patashu/8bit-progressive-metal-fading-world http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1) http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png |
09-21-2011, 12:26 AM | #13 | |
FFR Veteran
|
Re: Java help
Quote:
A few things to note: don't make variables for things like 'space', that's just bad practice. There's no use in making a variable for a string that you use only once. It uses extra space in memory that can be avoided. Best thing to do is just add an extra space after the word 'are' instead of concatenating a space to it. Less code, simpler to read. Also, you can declare and create objects on the same line without a problem. It's usually faster to do it like this anyways, cause the object gets set to null, then set the the new value with your method, but creating the object right away skips that 'null' step, thus making it faster (nanoseconds, but those count). There's also no point in rolling then getting the value, you can do that all in one function. roll() will now assign the randomized number to the number variable, then return the same value to your println() function (which called roll()). This last change is optional though, but I like to make my life easier by doing multi-purpose functions. You can still roll the same way even though it now returns a value. It just makes the roll() function more useful. Anyways, as to your actual question: Code:
class DieRoll { public static void main ( String [] args) { Die die = new Die(); for(int i = 0; i < 3; i++) System.out.println("Result: " + die.roll()); } } Code:
Result: 3 Result: 4 Result: 1 A for loop is basically works like this: "Starting from 0 (declare your iterator with int i = 0; ), do the following operations (code under the for loop code), and then increment the iterator by one afterwards (i++), until the condition (i < 3; ) is no longer true." So the first result will happen when i = 0, the second result will happen when i = 1, and the third result will happen when i = 2. When i = 3, the for loop condition is no longer true, so it will not print a fourth result. Let me know if you have any questions. Btw I got A+ in both my java classes last year, I know my stuff. I'm definitely willing to help out some more with whatever questions you might have, I like explaining programming concepts Last edited by MaxGhost; 09-21-2011 at 12:43 AM.. |
|
09-21-2011, 12:41 AM | #14 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
From this I am wondering where the die.roll() went.
|
09-21-2011, 12:43 AM | #15 |
FFR Veteran
|
Re: Java help
It's two separate classes, I only included the DieRoll class, but that code assumes you're compiling it with the Die class as well. The first three lines were just comments, assume those are not there as well.
Does that answer your question? Your question wasn't very clear. |
09-21-2011, 12:46 AM | #16 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
This was my old code
Code:
class DieRoll { public static void main ( String [] args) { String space; space = " "; Die die; die = new Die(); die.roll(); System.out.println("Your Results are" + space + die.getRoll()); } } But then you changed it to this but you didn't add the die.roll() part and I was wondering where it went. Code:
class DieRoll { public static void main ( String [] args) { Die die = new Die(); for(int i = 0; i < 3; i++) System.out.println("Result: " + die.roll()); } } Last edited by FFRGreen; 09-21-2011 at 12:49 AM.. |
09-21-2011, 12:49 AM | #17 |
FFR Veteran
|
Re: Java help
Like I explained, you don't need it anymore the way I changed your roll() function.
Originally, roll() just randomized a number between 1 and 6, and gave that value to the 'number' variable. Now, roll() does the same as above, but also returns the value when called. What that means is that when you try to print 'die.roll()', it will replace that with the number it randomized. |
09-21-2011, 12:51 AM | #18 | |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
Quote:
Also thanks to everyone else that helped me. |
|
09-21-2011, 01:07 AM | #19 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
|
Re: Java help
Now integrate it again without reviewing your old code.
MaxGhost was very helpful, but you should try to figure it out yourself as well.
__________________
|
09-21-2011, 01:18 AM | #20 |
Greeeeeeeeeeeeeeeeeeeeeen
|
Re: Java help
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|