Results 1 to 9 of 9
  1. #1
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default Java Program (rolling the dice)

    I'm not even sure if this is where i'm supposed to ask this.
    And i also know this is really simple, but i missed the first two weeks of class as i registered late and all of a sudden i have homework assignments. And i have no idea what to do.

    The question that's asked is:
    Write a program that simulates rolling two dice using the following steps:

    Prompt the user for the number of sides for two dice.
    “Roll” the dice three times by generating a random number between 1 (inclusive) and the number of sides (inclusive).
    Keep track of the sum of the rolls for each die and output the sum and average for each die.

    and this is how much i've gotten so far


    // Subhan Jamil
    // dice.java

    import java.util.Random;

    public class dice {

    public static void main (String[] args) {


    int die1;
    int die2;

    java.util.Scanner sc = new java.util.Scanner(System.in);
    int num_sides = Scanner.nextInt();




    If any 1 can please help me, or better yet talk to me personally to teach me a little i would greatly appreciate it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This won't work:
    Java Code:
        java.util.Scanner sc = new java.util.Scanner(System.in);
        int num_sides = Scanner.nextInt();
    Because you're trying to call a Scanner method off of the class, Scanner, and you can only do that with static methods. Instead you must call nextInt off of a Scanner object, but fortunately, you have created just such a creature on the line above. So to fix your current problem do this:

    Java Code:
        java.util.Scanner sc = new java.util.Scanner(System.in);
        //int num_sides = Scanner.nextInt();// no good.  Can't call nextInt() off of the Scanner class
        int numSides = sc.nextInt(); // better -- you can call nextInt() off of a Scanner object, sc
    If any 1 can please help me, or better yet talk to me personally to teach me a little i would greatly appreciate it.
    We'll be happy to help you with errors in your code, but you'll find that the limitations of the forum and of our other time commitments (we're all volunteers after all), mean that we can't be a substitute for your teacher or a decent tutor, or for your studying and learning. And that's the first thing you must do: study and learn from your notes and your books before you attempt to start coding, else you're in for much pain.

    Good luck!

  3. #3
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This won't work:
    Java Code:
        java.util.Scanner sc = new java.util.Scanner(System.in);
        int num_sides = Scanner.nextInt();
    Because you're trying to call a Scanner method off of the class, Scanner, and you can only do that with static methods. Instead you must call nextInt off of a Scanner object, but fortunately, you have created just such a creature on the line above. So to fix your current problem do this:

    Java Code:
        java.util.Scanner sc = new java.util.Scanner(System.in);
        //int num_sides = Scanner.nextInt();// no good.  Can't call nextInt() off of the Scanner class
        int numSides = sc.nextInt(); // better -- you can call nextInt() off of a Scanner object, sc

    We'll be happy to help you with errors in your code, but you'll find that the limitations of the forum and of our other time commitments (we're all volunteers after all), mean that we can't be a substitute for your teacher or a decent tutor, or for your studying and learning. And that's the first thing you must do: study and learn from your notes and your books before you attempt to start coding, else you're in for much pain.

    Good luck!
    hey, yes i'm doing that. But it's just the first two weeks where i missed the basic things which is why it's taking me to long to get this. I'm going to catch up this week, just need help on finding out how to do a few programs.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by genocist View Post
    hey, yes i'm doing that. But it's just the first two weeks where i missed the basic things which is why it's taking me to long to get this. I'm going to catch up this week, just need help on finding out how to do a few programs.
    Again, we can help you with your work, but we cannot be a substitute for studying, nor can we do your work for you. So best of luck and keep on plugging! With hard work you can succeed.

  5. #5
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, we can help you with your work, but we cannot be a substitute for studying, nor can we do your work for you. So best of luck and keep on plugging! With hard work you can succeed.
    yep, trying hehe. appreciate the help, i'm not asking you to do my work. Was just asking help. As the notes don't cover this program and the book tends to be a little confusing. :o

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by genocist View Post
    yep, trying hehe. appreciate the help, i'm not asking you to do my work. Was just asking help. As the notes don't cover this program and the book tends to be a little confusing. :o
    No problem. Keep working at your program and please feel free to come on back to show us any further problems you may have with the code or any specific questions you may have, but remember, the more specific the question, the better usually our answer. We'll be happy to help. Again best of luck to you!

  7. #7
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    Thx, I realized it was wrong to ask to do the thing. AS i won't learn anything, good response and friendly. :)

  8. #8
    Joker2u is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default A start

    public class dice{ ... }
    first question what compiling program are you using that allows you to put the name of the class in lowercase? or did you just throw something together in hopes of someone replying to this and helping you??

    first...
    i would make the class die not dice you can create 2 instance of the die which will make 2 dice.
    public class Die{

    private int numberOfSides;
    private int numberOneRolled;
    private int numberTwoRolled; the numbers rolled can be determined by
    a pseudo-random number generator
    found in the API--Random.
    here is the link for that... Java 2 Platform SE v1.4.2
    look over the specification for creating a new random.
    next you need a constructor to make a die.
    public Die(int numberOfSides){
    this.numberOfSides = numberOfSides; <-- initializes the die with the
    specified number of sides and sets
    that number to the instance nOSides
    }

    next queries:

    /**
    *returns the number of sides the die has.
    */
    public int numberOfSides(){
    return numberOfSides;
    }


    /**
    *returns the first number rolled
    */
    public int numberOneRolled(){
    return numberOneRolled;
    }

    ^^ same for numberTwoRolled



    next commands:
    /**
    *this command sets the number of sides the die has.
    */
    public void setNumberOfSides(int number){
    this.numberOfSides = number;
    }

    /**
    *this command rolls the dice.
    */
    public void rollDice(Die die1, Die die2){
    Random r1 = new Random();
    if (die1.numberOfSides() < r1.nextInt(numberOfSides) + 1){
    die1.numberOfSides() = numberOfSides - 1;
    }

    }
    }

    you need to create a TUI after this but this is the just and i hope it will get you going in the right direction.

  9. #9
    genocist is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    hey, i really appreciate it. I kind of did just throw something together but it did take me almost 3 hours from getting it from the book. Again, it's just one week because i'm new to the class and have 5 other classes so it's a little hard to catch up.

Similar Threads

  1. Dice game issue- any Java gamblers ableto help?:P
    By mambalamba in forum New To Java
    Replies: 2
    Last Post: 12-17-2009, 06:49 PM
  2. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 11:50 AM
  3. Log4j problem - Logs are rolling into the previous date file
    By vaibhavborole in forum Advanced Java
    Replies: 0
    Last Post: 04-16-2009, 03:33 PM
  4. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 01:01 AM
  5. java help please- two sided dice
    By pat8 in forum Advanced Java
    Replies: 3
    Last Post: 07-01-2008, 02:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •