Adblock breaks this site

JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

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

  1. paosshole

    paosshole Newcomer

    Joined:
    Dec 18, 2009
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  2. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    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.
     
  3. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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.
     
  4. runeowner

    runeowner Forum Addict
    Banned

    Joined:
    Jan 23, 2007
    Posts:
    456
    Referrals:
    0
    Sythe Gold:
    0
    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    Can you tell us why your teacher wants you to use JOption or BufferedReader?
     
  5. pkwithpink

    pkwithpink Apprentice
    Banned

    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0
    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);
    
     
  6. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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.
     
  7. pkwithpink

    pkwithpink Apprentice
    Banned

    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0
    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);
     
  8. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    Yes, it would. I was overthinking it.
     
  9. pkwithpink

    pkwithpink Apprentice
    Banned

    Joined:
    Nov 14, 2008
    Posts:
    770
    Referrals:
    0
    Sythe Gold:
    0
    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

    :)

    System.out.println("LOL");
     
  10. runeowner

    runeowner Forum Addict
    Banned

    Joined:
    Jan 23, 2007
    Posts:
    456
    Referrals:
    0
    Sythe Gold:
    0
    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..
     
  11. fromdhood101

    fromdhood101 Forum Addict
    Banned

    Joined:
    Sep 26, 2007
    Posts:
    440
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  12. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

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


 
 
Adblock breaks this site