Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Dice Counter

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Dice Counter

    Hi Im am basicly new to java and the these forums and i am having a problem here.
    The thing is im still in high school and we started to learn java and we are doing a dice program and what we have to do is that we have to make it roll a pair of six sided die and we also have to make it count how much 1s, 2s, 3s, 4s, 5s, and 6s showed up out of said amount of rolls which is what i am having the problem with

    heres my code:

    import java.util.Random;
     
    public abstract class DiceRoll {
     
        public static void main(String[] args) {
     
            int Dice1 = 0;
            int Dice2 = 0;
     
            Random generator = new Random();
     
            int counter = 0;
            int nOne = 0;
            int nTwo = 0;
            int nThree = 0;
            int nFour = 0;
            int nFive = 0;
            int nSix = 0;
     
            for(int i=0; i < 1200 ; i++) {
                Dice1 = (int)(Math.random()*6) + 1;
                Dice2 = (int)(Math.random()*6) + 1;
     
            } // end for
     
            System.out.println("There were " + nOne   + " ones,  " + nTwo   + " twos, "
                + nThree +      " threes, "    + nFour  + " fours, " + nFive  + " fives, and, "
                + nSix   +      " sixs "       + "were rolled out of ");
        } // end main
     
    } // end class DiceRoll

    Thank you for help


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Dice Counter

    The problem is that you're assigning different numbers to Dice1 and Dice2 for each pass of the loop but you don't have any code to increment nOne, nTwo, nThree and the rest so when you print them out, they still have the original 0 value you've assigned. You need to put code in to test the value of Dice1 and Dice2 in each loop so if its 1 you do nOne++ and so on.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Counter

    Can you explain it in a more newbier form and possible do an example. thx

  4. #4
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Dice Counter

    In your loop you are assigning random numbers to Dice1 and Dice2 which works ok.
    for(int i=0; i < 1200 ; i++) {
                Dice1 = (int)(Math.random()*6) + 1; 
                Dice2 = (int)(Math.random()*6) + 1;
     
            }

    The problem is that the loop then runs again and you assign two different random numbers to Dice1 and Dice2 and the original numbers are lost.

    You have the variables to count the number of times each number is rolled but these variables are never used. They are assigned a value of 0
            int nOne = 0;
            int nTwo = 0;
            int nThree = 0;
            int nFour = 0;
            int nFive = 0;
            int nSix = 0;

    and then not used again so when you print them, they are still 0.

    The best way I can see is either if statements or switch statements which can read the current value of Dice1 and add one to the right variable.

    for(int i=0; i < 1200 ; i++) {
                Dice1 = (int)(Math.random()*6) + 1;
                if(Dice1==1)
               {
                      nOne++;
               }
               if(Dice1=2)
              {
                      nTwo++;
               }
    and so on for Dice1 = 3, 4, 5 and 6
    You could also use switch statements, link to a tutorial below.

    Switch Statements

    You would have to do the check the values for Dice2 as well.

  5. The Following 2 Users Say Thank You to samfin For This Useful Post:

    JavaPF (January 26th, 2011), Xxl0n3w01fxX (January 26th, 2011)

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Counter

    Thnx This Help Alot

    kk another Question:

    instead of doing this:

     for(int i=0; i < 1 ; i++) {
                Dice1 = (int)(Math.random()*6) + 1;
                if(Dice1==1)
                {
                    nOne++;
                }
                if(Dice1==2)
                {
                    nTwo++;
                }
                if(Dice1==3)
                {
                    nThree++;
                }
                if(Dice1==4)
                {
                    nFour++;
                }
                if(Dice1==5)
                {
                    nFive++;
                }
                if(Dice1==6)
                {
                    nSix++;
                }


    Can i add an Else for example.

     for(int i=0; i < 1 ; i++) {
                Dice1 = (int)(Math.random()*6) + 1;
                if(Dice1==1)
                {
                    nOne++;
                }
                Else {
                    (Dice1==2)
                {
                    nTwo++;
    and so on?
    Last edited by Xxl0n3w01fxX; January 26th, 2011 at 11:30 AM.

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Lightbulb Re: Dice Counter

    Quote Originally Posted by Xxl0n3w01fxX View Post
    Can i add an Else for example.

     for(int i=0; i < 1 ; i++) {
                Dice1 = (int)(Math.random()*6) + 1;
                if(Dice1==1)
                {
                    nOne++;
                }
                Else {
                    (Dice1==2)
                {
                    nTwo++;
    and so on?
    The code is wrong but I get what you are trying to do. You can't do this because your basically saying, if you roll a 1, Dice1 becomes 1, but if anything else is rolled, Dice1 becomes 2. What if a 3 was thrown?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    Xxl0n3w01fxX (January 26th, 2011)

  9. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Dice Counter

    For what it's worth, this sure does sound like a job for an array or a List.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dice Counter

    oh ok Thx For the Help Anyways JavaPF

Similar Threads

  1. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM
  2. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:13 PM
  3. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 11:56 AM