Nick Cantara wrote: Not sure about this last one, but the program should have a class called Die that stores the data for each die.

This is what I have so far and I'm lost on it There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors. Forum: Beginning Java. Roll the Dice program. Nick Cantara.

So I'm writing an application that rolls two six-sided dice, displaying the results after each roll and asking the user if they would like to roll again. It is supposed to show Roll 1, Roll 2, Roll 3, etc, but I am having problems with it showing that based on my counter variable. I have 5 classes created. Below is what I have for code so far. This is what I have so far and I'm lost on it.

Jeff Verdegan. I like Hi, and welcome to the Ranch! Wha specific problem are you having?

**The Probability and the Sum of Two Dice**

Also, that's a lot of code, and the vast majority of it is not related to your problem. It will be easier for people to help you and easier for you to help yourself!

### Subscribe to RSS

Winston Gutkowski. Right, well I'm assuming your Die class is supposed to model a single die, so why would you have a ' sum ' field? Also, it would appear that you're using the ' d ' field to hold the number of sides, so why not call it ' numberOfSides '? Giving things meaningful names is a very important part of good programming. If it's the random number stuff you're stuck on, have a look at the Random class java. You are writing too much code at once.

Before you ever write a single line of code, you should analyze the problem and come up with all the individual things you need it to do. For example, my first pass at this assignment would be: 1 Generate an integer in the range 2 compute the sum of two numbers 3 Display a result 4 keep track of the number of rolls 5 ask the user something 6 Get input from a user 7 validate input 8 loop around and do it all again if needed I'd make sure I could do each of the above steps by them selves before trying to combine them.Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X.

X is the summation of values on each face when all the dice are thrown. The Naive approach is to find all the possible combinations of values from n dice and keep on counting the results that sum to X.

Why DP approach? The above problem exhibits overlapping subproblems. See the below diagram. Also, see this recursive implementation. Let there be 3 dice, each with 6 faces and we need to find the number of ways to get sum Please take a closer look at the above recursion.

Hence, storing the results of the solved sub-problems saves time. Following is implementation of Dynamic Programming approach. We can add the following two conditions at the beginning of findWays to improve performance of the program for extreme cases x is too high or x is too low. This article is compiled by Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Writing code in comment? Please use ide. Dice Throw DP Optimal Strategy for a Game DP Count of subsets with sum equal to X Median of sliding window in an array Eggs dropping puzzle Set 2 Split the given string into Primes : Digit DP Sum of GCD of all possible sequences Maximum sum path in a Matrix Count of numbers in range which are divisible by M and have digit D at odd places Minimize total cost without repeating same task in two consecutive iterations Minimum increment or decrement required to sort the array Top-down Approach Maximum size of square such that all submatrices of that size have sum less than K Minimum window size containing atleast P primes in every window of given range Count maximum occurrence of subsequence in string such that indices in subsequence is in A.

One extra. The entries in 0th row and 0th column. One extra row and column are used for simplicity. Number of dice is directly used as row index and sum is directly used as column index. The entries in 0th row and 0th column are never used. Python3 program to find the number of ways to get sum 'x' with 'n' dice. The main function that returns number of ways to get sum 'x'. Create a table to store results of subproblems. Fill rest of the entries in table using recursive relation.

Uncomment above line to see content of table.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Review Stack Exchange is a question and answer site for peer programmer code reviews.

It only takes a minute to sign up. I'm a beginner trying to create a dice game according to the specification given below. This is an improved version of my program based on the feedback given by users on my original question.

Again, rather than asking other people to correct my code, I would much prefer to be given hints or general principles as to how my code could be improved. I have been told that there is a hundred way to write a program, a thousand way to break it and a million way to improve it.

Please feel free to pick apart my code and give me any suggestions on areas in my program that could be improved, especially in areas such as code structure and logic. The Dice Throwing Game begins with a welcome message followed by a menu with the following options :. Option 1 asks players to enter their names. After the names are set up, the game asks for a maximum score. The default maximum score should be set to points. When this option is chosen, the computer generates 2 random numbers between ie.

It then updates players' scores accordingly. The scoring rules for each "round" are as follows :. The menu must be displayed repeatedly after each operation, until the user chooses Option 5. Inputs other than should be rejected, with appropriate error messages printed.

Also, why is this member package private? Ideally you would not have an endless loop which you break out from but instead something that tells you if the game is over or not, like this:. That's quite a bad name for this function, because it does not select an option, it does execute an action. Also I'd argue that the parameters name should be turned around to selectedOption. Even better would be if you'd represent your options as an enum, that would remove all the magic numbers from your code, but would, of course, add some logic to map read integers to enum values.

Assuming that you want at least one player, obviously. You never check if there are enough players, though.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here.

Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm making a dice rolling game! The sum will be taken from the 2 numbers and used to decide what is next.

If user's sum is 2,3,12 then they lose. If the sum is 7,11 then they win. If sum is 4,5,6,8,9,10 then the program automatically rolls the dice again until the user wins or loses.

Here is my code so far:. You should use a while loop: The dice are rolled again and again until the player has won or lost then, break ends the while loop. Learn more. Dice Rolling java program Ask Question. Asked 5 years, 6 months ago. Active 2 years, 4 months ago. Viewed 65k times. Hope you brought lots of CASH!

## Subscribe to RSS

Jk1 9, 9 9 gold badges 47 47 silver badges 63 63 bronze badges. May I suggest a while loop? Your title is not useful. Please edit it to reflect the issue at hand. Why do you run every thing in the main method? Start a daemon thread to run the engine.

Diwas, Sorry this is not a solution but a question. I was working on a similar problem and did not understand the code in my textbook. Active Oldest Votes. The Overflow Blog. Featured on Meta. Feedback on Q2 Community Roadmap. Technical site integration observational experiment live on Stack Overflow. Dark Mode Beta - help us root out low-contrast and un-converted bits. Question Close Updates: Phase 1. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I need to write a simple java program "Roll the Dice" with special variables: When the user rolls the dice, the application rolls two dice, displays the results of each, and asks if the user wants to roll again. Write a program that has the Main method and a separate method for the random number generator. Create a 4 integer variables to store two dice, sum of the two dice, and one for the number of times the dice are rolled. I spend an hours to try make it right but nothing really works.

Please, if someone can help me to do correct this program that I have some issue with, the result should look more or less like this:.

Learn more. Roll two dice java program Ask Question. Asked 6 years, 6 months ago. Active 4 years, 3 months ago. Viewed 12k times. Random; import java. Scanner; import javax. SpringLearner This is not a reasonable indentation pattern, there is an extra bracket so I doubt it will compile, and I expect you wanted the dice rolling stuff to be inside the while loop or defined in a helper method called from there.

If in doubt do one thing at a time and test that it works. The step by step approach to programming is really the way to go. Fix your identation in code. It will help you much better finding syntax and some logic errors.

Active Oldest Votes.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I am having trouble calculating out the frequency of the sum of the rolling of a pair of dice. I am told I have to use a one dimensional integer array to count the number of times each possible sum appears in rolls.

I am unsure what I am supposed to do since we just started going over arrays. You would separate a sheet of paper into 12 columns one for 1, one for 2, etc. You would then roll the dices times, and each time you would get a 7, for example, you would add a mark in the column 7. Then you would count the marks in each column, and divide the sum by to get their frequency.

Just do the same in Java: have an int[] counts array of 12 elements. Then do a loop from 0 to At each iteration, roll the dices, and increment the array element corresponding to the value you got. So, for example, if you got 7th, yo would increment the 7th element of the array.

Perhaps the instructions ask that you create all the separate methods but I, personally, would code it something like the following.

I had a friend help me in class. I apologize for my previous comments. I did not look through the code well enough to understand what it was doing.

From what I understand now by looking at both your code and JB nizet's logic is that the switch statement takes the value of the sum that corresponds to each element of the array and increments depending on the number of corresponding sum values that are found.

I had originally declared and initialized the array inside of the for loop as well as the print statement hence why I was getting values. I then moved my array declaration and assignment outside of the loop so I would be able to use it throughout the program then I had fixed my while loop to go from [0, 10] and added two to count in order to get the possible sum values [2, 12] then printed out the frequency by passing the value of count through the frequency array.

When writing the equation to calculate out the percentage I ran into the issue of getting 0.Java program to calculate the sum of N numbers using arrays, recursion, static method, using while loop. Here is the complete Java program with sample outputs. You can learn more tutorials here and Java interview questions for beginners. With the following program, you can even print the sum of two numbers or three numbers up to N numbers.

Using Arrays. How this program works: we are using arrays to store the values first. Say 5: Now the next step is to enter those numbers in a series order. Once it was done, the program automatically adds all your two numbers or three numbers like up to N numbers. Here is another method using recursion: A recursion is a function call itself.

Here is the sample program to print the sum of N numbers in Java. Using Static Method. Another example program using the static method.

How to calculate? Just summing of two numbers or three numbers or up to N numbers. Sum Of N Numbers Program 1. Scanner. Enter how many numbers you want sum. Enter the 4 numbers. Java Tutor. Copyrighted Protected.