By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,643 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,643 IT Pros & Developers. It's quick & easy.

Java program that takes user input to roll 2 die

P: 1
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?
Jul 13 '13 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
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/misunderstanding 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):
    Expand|Select|Wrap|Line Numbers
    1. Random rand = new Random();
    2. 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.print(...) or System.out.println(...).
  • 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. :-)
Jul 14 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.