JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

Discussion in 'Programming General' started by paosshole, Dec 18, 2009.

JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT
  1. Unread #1 - Dec 18, 2009 at 2:51 AM
  2. paosshole
    Joined:
    Dec 18, 2009
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0

    paosshole Newcomer

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    I need help, how do I make a basic dice game that uses JOption or BufferedReader, the dice rolls 20 times and prints the result, please help me..T_T
     
  3. Unread #2 - Dec 18, 2009 at 9:20 AM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    Do you have any code yet? This is a very simple program, but we are not just going to give it to you.
     
  5. Unread #3 - Dec 18, 2009 at 10:32 AM
  6. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    As a plan to make your program:
    - create a method that will return a random number between 1 and 6
    - in your main() method, create a loop that will call the above method 20 times and print the result

    Also, what is the purpose of the JOptionPane or BufferedReader (perhaps to figure out how many rolls to do)? If it is, look into the java.lang.Integer class to find out how to parse an int from a String.
     
  7. Unread #4 - Dec 19, 2009 at 9:25 PM
  8. runeowner
    Joined:
    Jan 23, 2007
    Posts:
    456
    Referrals:
    0
    Sythe Gold:
    0

    runeowner Forum Addict
    Banned

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    Can you tell us why your teacher wants you to use JOption or BufferedReader?
     
  9. Unread #5 - Dec 20, 2009 at 1:20 AM
  10. pkwithpink
    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0

    pkwithpink Apprentice
    Banned

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    This is very simple, at least show that you put some effort trying to write this program and we can help you along the way. You will want to create a new Random.


    Here I wrote you a random integer generator took 3 seconds.
    Just loop it 20 times and you are set. What do you need with JOption or BufferedReader? Isn't the simplest answer almost always the right one?

    Code:
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(6);
    System.out.println(randomInt);
    
     
  11. Unread #6 - Dec 20, 2009 at 1:37 AM
  12. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    This wouldn't work, as 0 would be a possible value, and 6 wouldn't.
    Something along the lines of:
    Code:
        private static int rand(int min, int max) {
            return ((int)((Math.random() * (max - min + 1)) + min));
        }
    Would work.

    EDIT: Or something like
    Code:
    private static int rand() {
        int i = rand.nextInt(7);
        if(i != 0)
            return i;
        return rand();
    }
    In which rand is an instance of java.util.Random.
     
  13. Unread #7 - Dec 20, 2009 at 1:41 AM
  14. pkwithpink
    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0

    pkwithpink Apprentice
    Banned

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    I am rusty on the Random method, haven't had to use it in quite a while. Thanks for the correction.

    Wouldn't this just work?

    Code:
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(6);
    int rolledint = randomint + 1;
    System.out.println(rolledint);
     
  15. Unread #8 - Dec 20, 2009 at 1:55 AM
  16. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    Yes, it would. I was overthinking it.
     
  17. Unread #9 - Dec 20, 2009 at 2:06 AM
  18. pkwithpink
    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0

    pkwithpink Apprentice
    Banned

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    :)

    System.out.println("LOL");
     
  19. Unread #10 - Dec 22, 2009 at 12:45 AM
  20. runeowner
    Joined:
    Jan 23, 2007
    Posts:
    456
    Referrals:
    0
    Sythe Gold:
    0

    runeowner Forum Addict
    Banned

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    This would be your best solution :

    Code:
    Random randomGenerator = new Random();
    int rolls[] = new int[20];
    
    for(int i =0; i<20; i++{
    int randomInt = (randomGenerator.nextInt(6))+1;
    rolls[i]=randomInt;
    [COLOR="YellowGreen"]// this is if you want it stored in an array[/COLOR]
    System.out.println(rolls[i]);
    }
    ----

    I hope your problems are solved, you havent posted back yet..
     
  21. Unread #11 - Dec 24, 2009 at 8:20 AM
  22. fromdhood101
    Joined:
    Sep 26, 2007
    Posts:
    440
    Referrals:
    0
    Sythe Gold:
    0

    fromdhood101 Forum Addict
    Banned

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    Wtf is that shit, why would you loop something 20 times for nothing to get a random integer?

    Code:
    	public static int displayRandomInteger(int max) {
    		return (int)(java.lang.Math.random() * (max + 1));
    	}
    Easy as that.

    Edit: Forgot about the dice thing, stupid me :p
     
  23. Unread #12 - Dec 24, 2009 at 8:26 AM
  24. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    ^It needs to loop 20 times... >_>.
     
< Efficient Auto Talker | Searching a String >

Users viewing this thread
1 guest


 
 
Adblock breaks this site