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 score program

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Dice score program

    This is a small project for my class. Need to write a code that takes in two integers in the main class. One integer is the number of dice to be thrown, and the second is a score. The program needs to throw the specified number of dice 100,000 times then report what percent of the time the score enter appeared. So say 4 was enter for the number of dice to be thrown at once, and 6 was the score entered. The program needs to count the number of times the score of 6 occurs when 4 dice are thrown 100,000 times.

    Here's my code:

    public class SimpleDice{
     
      public int tossDie(){
        return(1 + (int)(Math.random()*6));
      }
     
      public int throwSetDice(){
        int score = 0;
        for(int j = 0; j <= diceCount; j++){
          score = (score + tossDie());
        }
        return score;
    }
    }

    import java.util.Scanner;
     
    public class DiceTester{
      public static void main(String[] args){
     
        int diceCount;
        int desiredScore;
     
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of Dice:");
        diceCount = scan.nextInt();
        System.out.println("Enter score to count:");
        desiredScore = scan.nextInt();
        System.out.println("dice: " + diceCount + " score: " + desiredScore);
     
        SimpleDice d = new SimpleDice();
        int scoreCount = 0;
     
        for(int i = 0; i < 100000; i++){
          d.throwSetDice();
          if(d.throwSetDice() == desiredScore){
            scoreCount += 1;
          }
        }
        System.out.println("Result: " + (scoreCount/100000));
      }
    }

    I'm getting one error in my SimpleDice class (9th line) in the for loop reading:
    "diceCount cannot be resolved to a variable"

    How do I call the amount of dice to be thrown at once in the SimpleDice class?


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Dice score program

    I would like to be able to utilize the SimpleDice class more, but I edited the DiceTester class to do the work of the throwSetDice() method. Now I'm running in to another problem though. When trying to print the result (scoreCount/100000), I keep getting an answer of 0. Yet if I print just the scoreCount int, it shows it does have a value but java is not dividing it by 100000. I tried using int, double, long, and i dont know what else could be used. New code:

    import java.util.Scanner;
     
    public class DiceTester{
      public static void main(String[] args){
     
        int diceCount;
        int desiredScore;
        int diceThrows = 100000;
     
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of Dice:");
        diceCount = scan.nextInt();
        System.out.println("Enter score to count:");
        desiredScore = scan.nextInt();
        System.out.println("dice: " + diceCount + " score: " + desiredScore);
     
        SimpleDice d = new SimpleDice();
        int scoreCount = 0;
     
        for(int i = 0; i < diceThrows; i++){
          int score = 0;
          for(int j = 0; j < diceCount; j++){
            score = (score + d.tossDie());
          }
            if(score == desiredScore){
              scoreCount += 1;
            }
        }
     
        double result = (scoreCount/diceThrows);
        System.out.println(result);
        System.out.println(scoreCount);
      }
    }

    note that the new code only uses the tossDie() method from the SimpleDice class.
    Last edited by ksahakian21; April 23rd, 2012 at 01:48 AM.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Dice score program

    Quote Originally Posted by ksahakian21 View Post
    I would like to be able to utilize the SimpleDice class more, but I edited the DiceTester class to do the work of the throwSetDice() method. Now I'm running in to another problem though. When trying to print the result (scoreCount/100000), I keep getting an answer of 0. Yet if I print just the scoreCount int, it shows it does have a value but java is not dividing it by 100000. I tried using int, double, long, and i dont know what else could be used. New code:
    Hello ksahakian21!
    Since the scoreCount and diceThrows variables are ints you're using integer division and floating point is not considered. There several ways to make it work. You can just cast the division to double
    double result = (double) (scoreCount/diceThrows);

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Dice score program

    Thanks I should have realized that.
    Does anyone know of a way to get my original code to work? Utilizing the throwSetDice method in the SimpleDice class.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Dice score program

    Not sure if this is the right way to go about this or not, but I'm trying to make a new method in the SimpleDice class which (might?) allow me to call the diceCount variable from the DiceTester class to the SimpleDice class.

    public class SimpleDice{
     
    public int howMany(int the_count){
        return the_count;
      }
     
      public int tossDie(){
        return(1 + (int)(Math.random()*6));
      }
     
      public int throwSetDice(){
        int score = 0;
        for(int j = 0; j <= c.howMany; j++){
          score = (score + tossDie());
        }
        return score;
    }
    }

    import java.util.Scanner;
     
    public class DiceTester{
      public static void main(String[] args){
     
        int diceCount;
        int desiredScore;
     
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of Dice:");
        diceCount = scan.nextInt();
        System.out.println("Enter score to count:");
        desiredScore = scan.nextInt();
        System.out.println("dice: " + diceCount + " score: " + desiredScore);
     
        howMany c = new howMany(diceCount);
     
        SimpleDice d = new SimpleDice();
        int scoreCount = 0;
     
        for(int i = 0; i < 100000; i++){
          d.throwSetDice();
          if(d.throwSetDice() == desiredScore){
            scoreCount += 1;
          }
        }
        System.out.println("Result: " + (scoreCount/100000));
      }
    }
    Last edited by ksahakian21; April 23rd, 2012 at 01:50 PM.

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dice score program

    So basically you want to make a class that simulates a dice being rolled?

    I think you're overcomplicating the whole thing, it doesn't have to be as complicated as you make it out to be, just think simple.

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Dice score program

    Quote Originally Posted by ksahakian21 View Post
    Not sure if this is the right way to go about this or not, but I'm trying to make a new method in the SimpleDice class which (might?) allow me to call the diceCount variable from the DiceTester class to the SimpleDice class.
    I don't think the last posted code will compile. If I were you , I would modify the throwSetDice() method in your first post to take the expected result. So you have:
    public int throwSetDice(){
        int score = 0;
        for(int j = 0; j <= diceCount; j++){
          score = (score + tossDie());
        }
        return score;
    }

    What if you make throwSetDice() take an int parameter and use it like diceCount?
    Then in the test class you can call throwSetDice() with diceCount which the user has entered as a parameter.
    Hope I was clear.

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

    ksahakian21 (April 30th, 2012)

  9. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dice score program

    Quote Originally Posted by andreas90 View Post
    [highlight=java]
    public int throwSetDice(){
    int score = 0;
    for(int j = 0; j <= diceCount; j++){
    score = (score + tossDie());
    }
    return score;
    }
    I'm pretty sure the for loop will roll the dice and extra time, so if diceCount was 4, it would roll 5 times because you put less than OR equal to diceCount, it should only be less then. The statement would only be correct if you started at 1.

Similar Threads

  1. Need to write a public instance method for a simple dice program
    By akira25 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 9th, 2012, 04:46 AM
  2. Help with creating a dice rolling program in Java
    By lilmiss in forum Object Oriented Programming
    Replies: 4
    Last Post: October 26th, 2011, 09:27 PM
  3. Java Dice Program
    By ebone in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 22nd, 2011, 11:07 PM
  4. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM
  5. Help with score board!
    By vlan in forum Java Applets
    Replies: 0
    Last Post: June 2nd, 2010, 04:34 AM