473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Java program that takes user input to roll 2 die

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
1 2017
Nepomuk
3,112 Expert 2GB
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

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

Similar topics

3
by: Ganesh | last post by:
hi pals, I like to know ,how to generate a mouse event through the java program .. the user should not need to trigger the mouse event.the program should control the mouse to generate a mouse...
2
by: Kyle E | last post by:
Ok, I have a little tiny problem, a beginner problem, that I am overlooking. I am writing a Information Gathering Program, that takes user input from scripted questions and prints them in a handy...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
3
by: satyakarvvk | last post by:
Hi everybody! Please help me to overcome below runtime exception. Actually it is a simple program on basics. I want to print odd nos upto which the user asks and after printing the task,...
1
by: Chris Carlen | last post by:
Hi: I'm writing a Python program, a hex line editor, which takes in a line of input from the user such as: -e 01 02 "abc def" 03 04 Trouble is, I don't want to split the quoted part where...
12
by: Tarique | last post by:
I have tried to restrict the no. of columns in a line oriented user input.Can anyone please point out potential flaws in this method? btw.. 1.I have not used dynamic memory allocation because...
1
by: Akinyemi | last post by:
Hi, I started learning Java a year ago. Before then I had written a Visual Basic Program that maintains student information. I now want to write the same program in Java. But I do not know how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.