Java program that takes user input to roll 2 die

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dubyer
    New Member
    • Jul 2013
    • 1

    Java program that takes user input to roll 2 die

    Hi everyone,

    I am taking Java for the first time(required for my degree), and I am looking for help with a program I need to create. I have searched the forums, but none of them meet the criteria that I am looking for.

    For my homework, I need to write a program that will roll 2 die based on the user input. The program needs to keep looping until the number of rolls has been reached. For my output, I need to display the roll number, the roll of the 2 die, and the sum of the 2 die.
    For example, if the user entered to have the program roll the die 10 times, I want my output to look something like this:

    Roll 1 - The first die comes up 5, the second die comes up 4, your total is 9.
    Roll 2 - The first die comes up 2, the second die comes up 3, your total is 5.
    Roll 3 - The first die comes up 6, the second die comes up 1, your total is 7.
    This will continue to go until the max roll is reached. In this case 10.

    Can anyone help me out?
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi Dubyer and welcome to bytes.com!

    Quick question about your example: You say it should roll 10 times but there are only 3 lines of response representing rolls. Of course, if you say that both dice are rolled 3 times each that would be 6 but still not 10. Is the example incorrect or am I missing/misunderstandin g something?

    Now, while we won't write your program for you we'll be happy to help you out of course. Can you show us what have you done so far or are you looking for tips on how to start?

    Generally speaking, I see four components to that task:
    • Getting input
    • Creating random numbers
    • Printing output
    • Repeating instructions a set amount of times

    So, here are a few thoughts:
    • Getting input is nicely explained in this article, so that should be easily dealt with.
    • Random numbers can easily be created with the Random#nextInt( n) function. Here's a quick example on how to create a number between 1 and 10 (both inclusive):
      Code:
      Random rand = new Random();
      int number = rand.nextInt(10) + 1;
      The reason to add a 1 at the end is because nextInt(n) will create a random number between 0 and 10 exclusive. As you want one between 1 and 10 inclusive, you can just add 1.
    • Printing output is very simply done by functions such as System.out.prin t(...) or System.out.prin tln(...).
    • Repeating code is often done with the help of loops. In Java there are three types of loops: while, for and do-while. All three are explained in this article so check that out and decide which is best suited for your needs.

    Hope that helps. :-)

    Comment

    Working...