All Possible Outcomes Of Dice Roll Using Recursion
Nov 26, 2014
Here is what I have so far:
/**
* This class encapsulates a simple dice game. The number of dice and number of sides on those dice are given by instance variables. The outcomes ArrayList holds a list of all possible outcomes of throwing that number of dice with that number of sides.
*
* If there were 2 dice, each with 6 sides, then possible outcomes would include 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 2 1, 2 2, 2 3, and so on.
*
* Your task is to complete the methods that calculate the possible outcomes. One method calculates outcomes allowing for repeated numbers. One method calculates the outcomes of a fictional dice game where repeated numbers cannot occur.
*
* You must use recursion. This is a variation on the permutations problem from the book.
*/
public class Dice
{
private static int numberOfSides;
private static int numberOfDice;
public ArrayList<String> outcomes;
[code]...
I manage to calculate the numberOfOutcomes correctly, but then get a nullPointerException. Also, is there a way that I can use getNumberOf Outcomes without making it static? The testing code that the teacher provided is using it in another class and if I make it static, then it doesn't work in that file.
I am making a Dice Roll GUI and I have most of it down and I only need this constructor to work for the program to work (I think). I don't know where to place the constructor, I tried placing it around the RollButton class but it still didn't work and gave me java error constructor in class cannot be applied to given types
Here's my constructor:
private JPanel panel; public RollButton(JPanel panel){ this.panel = panel; }
what code I should add to my program in order to get these dice to roll. The program, according to my teacher, is CORRECT so far, so I'm not going to change anything. I just need to know how to roll the 2 dice I added in. I just want to get it done. No math.Random. The direction is: Roll each of the Dice by invoking the roll method on each Dice in the array.
My code:
//Dice.java package homework3; import java.util.Random; public class Dice { private int numberShowing; private int numberOfSides; private static final Random randomNumber = new Random();
Write a program that rolls two dice, adds the numbers and prints out the results. I have managed to do this but we have to make it a horizontal table instead of a vertical one. like the attached file "CORRECT", right now i only get the "NOTCORRECT". i have tried for hours to fix this but i can't.
import java.util.Random;
public class Statistikk { public static void skrivStatistikk() { Random rand = new Random(); int[] antall = new int[13]; {
[Code] .....
(Antall is basically frequency|side is the same as a face of a die)
Here is my code, does my code need improvement? Or is it my printf that is the issue? i've tried deleting the 's and adding the printf to the same line, but nothing worked.
I am trying to figure out how to simulate an ability score calculation for an RPG character creation. The way I want to do it is to have 5 d6 dice rolled and then the best three of the 5 rolls are added together for a maximum of 18 points and a minimum of 3 points.
Now I know how to generate random numbers, but I do not know how to have the best 3 picked out. What I have right now is super simple.
package javaapplication12; import java.util.Random; public class JavaApplication12 { /** * @param args the command line arguments */ public static void main(String[] args) {
[Code] ....
So this gives me my five rolls but then have the code pick out the best three every time the program is run is kind of an issue for me.
I'm finishing up this assignment, and I'm stuck. These are the last 2 instructions:
. Roll each of the Dice by invoking the roll method on each Dice in the array. . Keep track of the totals sum of all the dice in the array. Be sure to roll the dice array at least 10000 times.
How to finish it up?
Here's my code so far:
package homework3;
import java.util.Random; public class Dice { private int numberShowing; private final int numberOfSides; private static final Random randomNumber = new Random(); public Dice() { numberOfSides = 6;
After this problem is fixed, my math game will run. The answer variable is supposed to be equal to the cases in the second switch statement by random but I get errors whenever I try to initialize it this way. I have been writing this program for almost 5 days now and it is finally wrapping up. Why I can't map the answer variable to the second switch statement the way I want to?
package pkgnew; import java.util.Scanner; import java.util.Random; public class New { public static void main(String args[]) { //Declare and construct variables Random randomnum = new Random(); int fnum, snum, answer;
//Use a Switch statement to determine outcomes for letter grades switch (Letter) { case 'A': outFile.println(" Good Work! You have met the pre-req for CIS 220."); break; case 'B': outFile.println("
[Code] ....
is not printing the information on the next line in my output file.
The code I have below asks the user how many times you want to roll the dice. It rolls dice 1 and dice 2 randomly and gives the total. Now, I'm trying to keep track of the result of each roll using an array that is indexed by the sum of the roll of the two dice. Then I want to output my result in a table that shows each value (from 2 - 12)) and the number of times that value was rolled. I would like to do this preferably with the JTextArea class, but it doesn't have to be. I keep getting errors.The code below works for the dice rolling in the 1st paragraph. I took out all the bad code I was trying to use for the 2nd paragraph.
package Part2pack; import java.util.Random; import javax.swing.JOptionPane; class Dice{ public static void main (String args[]) { String input = " "; int count = 0,dice1,dice2;
I've written a die program, and i want the user to type any key in order to continue, how do i achieve this ?
I have already created a Die class, i just want to implement the main method now.
PHP Code:
package test;
/* This program demonstrates the use of a user-defined class
public class RollingDice { // Creates two Die objects and rolls them display the values. public static void main (String[] args) { Die die1 = new Die(); System.out.println("Roll the die ") } } mh_sh_highlight_all('php');
What should i add after System.out.println, So that the user types (any key, or types "Enter" for example, for the die to roll). i dont want the die to roll by itself i Want the user to interact with the program in order for it to roll.
public class PairOfDice { private Die die1, die2; public PairOfDice() { die1 = new Die(); die2 = new Die();
[Code] .....
I am struggling with the final part of my code to get the dice to roll 1000 times and tell me how many times a 7 was rolled. I am getting these errors in jgrasp
----jGRASP exec: javac -g PairOfDice.java
PairOfDice.java:34: error: <identifier> expected count = 0; ^ PairOfDice.java:35: error: illegal start of type for (rep = 1; rep <= 1000; rep++) {
I'm trying to get the program to roll a four sided die between 100 and 1000 times, depending on your input, and then displaying the answer.
public class Lab08 { public static void main(String[] args) { int v1 =0, v2 = 0, v3 = 0, v4 = 0; int n; char response; System.out.print("Enter number of rolls (100-1000): ");
Write a method called statistic that simulates the rolling of two dice 1000 times. (1000 times is a parameter of the method statistic).The program should have no input, and should use pseudo random numbers to simulate the rolls (one random number per die). Store the sum resulting from each roll of two dice, determine the number of times each 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 was rolled. The out produced by the program is:
public static void main(String[] args) { statistic(1000); } public static void statistic(int number) { Random randomNumbers = new Random();
[code]...
i have been trying for hours to make it look like the output above but no luck. I was wondering if there's any other way writing the code without using arrays.
The instructions are to "write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:
Java Code:
import java.util.Random; import java.util.Scanner; public class DiceProbability { public static void main(String[] args) { //Declare and initialize variables and objects //Input: ask user for number of rolls and number of sides on a die //Print heading for output table
[Code] ....
When I run it, I get something like this (just a part of my output):
Please enter the number of sides on a die: 6 Please enter the number of rolls: 2
I want to write a small Hi Lo Betting Dice program that satisfies these conditions :
A player places a bet on whether the sum of two dice will come up High (totalling 8 or higher), Low (totalling 6 or less) or Sevens (totalling exactly 7). If the player wins, he receives a payout based on the schedule given in the table below:
Choice Payout High 1 x Wager Low 1 x Wager Sevens 4 x Wager
The player will start with $100 for wagering. If the player chooses to wager $0 or if he runs out of money, the program should end. Otherwise it should ask him whether he's wagering on High, Low or Sevens, display the results of the die rolls, and update his money total accordingly.
some sample output:
You have 100 dollars. Enter an amount to bet (0 to quit): 50 High, low or sevens (H/L/S): H Die 1 rolls: 1 Die 2 rolls: 5 Total of two dice is: 6 You lost!
You have 50 dollars. Enter an amount to bet (0 to quit): 25 High, low or sevens (H/L/S): L Die 1 rolls: 6 Die 2 rolls: 2 Total of two dice is: 8 You lost!
You have 25 dollars. Enter an amount to bet (0 to quit): 12 High, low or sevens (H/L/S): H Die 1 rolls: 2 Die 2 rolls: 6 Total of two dice is: 8 You won 12 dollars!
I have written some code but now i am stuck at the logic ...
package com.peg.hilodice; import java.util.Scanner; public class DiceGame { public static void main(String[] args) { // TODO Auto-generated method stub int money = 100; int bet;
package dicedemo; public class DiceDemo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here
final int Die1_SIDES = 6; // Number of side of dice 1 final int Die2_SIDES = 6; // Number of side of dice 2 // Create two instances of the Die class.
[Code] ....
I need converting the number output to text: example one, two, three, etc.
I have this really a graphics program that I'm working on which displays a single die, and right now it is set to the default 6 sides. I need to change it to 12. Here is the program:
I am currently trying to roll 5 dice 100,000 times and output the number of times each possible total (of dots/pips) occurs. It seems to output the incorrect thing and isnt counting the occurrences properly.
public class lab_DiceRollTest { public static void main(String[] args) { int total; int total2; String valid; String isDifferent; int rolls = 100000; int d = 0;
I'm writing a program that is about rolling "dice" and getting the scores to add up to 100. There are two players in the game and each must take turns rolling dice choosing whether or not to keep rolling dice and accumulating more points during their turn while risking their points being lost by rolling a 1 or to add their turn points to their total points. The question I have is how would I exit the do while loop if the player chooses to add their turn score, thus adding their score and ending their turn?
Here is the coding so far.
Java Code:
import java.util.Scanner; import java.util.Random; public class Pig { public static void main(String[] args) { int die; int userTotalScore; int userTurnScore; int compTotalScore; int compTurnScore;
import java.util.Random; import java.util.Scanner; public class DiceGame6 { public static void main(String[] args){ Scanner stdIn = new Scanner(System.in); System.out.println("Welcome to Computer Dice!"); System.out.println("-------------------------------"); System.out.println("You will first roll your dice
[code]....
How would I get the code to count the number of times something occurred. In the end it's suppose to count how many times I played the game and how many times I've won vs lost.
my assignment is to code a program to simulate the rolling of 2 dice. I have most of it set but when I run it and have it print out how many times each number (2-12) was rolled, it displays the numbers 1 off. for instance it will display the number of 4s rolled next to the 5s.
here it is
import java.util.Scanner; //allows info to be inputted// import java.util.Random; //opens random number generator// public class RollTheDiceWithArray13 {
Write a program to simulate the rolling of two dice. The program should use Math.random (google how to use Math.random if needed) to roll the first die and should use Math.random again to roll the second die. The sum of the two values should then be calculated your program should roll 50,000 times. Use a single dimensional array to tally the numbers of times each possible sum appear(the possible values are 2 through 12, think about why?). Print the results in a tabular format.
Here's the code I'm working on so far:
public class RollingDice{ public static void main(String[] args){ int die1; int die2; int roll; die1 = (int)(Math.random()*6) + 1; die2 = (int)(Math.random()*6) + 1; roll = die1 + die2; System.out.println("The roll of the first dice is: " + die1); System.out.println("The roll of the second dice is: " + die2); System.out.println("Your total roll is: " + roll); } }
What am I missing at this point, and what parts did I do wrong to change it?
import java.util.Random; /** * A very basic Dice that can be rolled and represent int values */
public class Dice{ private Random rand; public Dice(){ rand=new Random(); } /**simulates rolling the dice*/
[Code] ....
Why the Random rand is initiated at the Constructor and not at roll? Like the code works for both but I remember our teacher telling us something about one uses more memory than the other.
I am having a problem with my java program. My goal is to request the user to enter how many times they want to roll a pair of dices. Each dice have numbers 1-6. When rolling the dices, I randomly pick a number from each dice and total the number. My goal is to calculate the number of times snake-eyes was rolled which is a total of 2 and total number of times a total of 7 was rolled. Here is my code. I am calling the rollDice method to perform the random pick and calculations. The error I am getting is at the bottom of the code.
package dice; import java.util.Scanner; import java.util.Random; public class Dice { public static void main(String[] args) { int numRolls; // number of rolls
[code]...
How many times would you like to roll the two dice? 1000 Exception in thread "main" java.lang.NullPointerException at dice.Dice.main(Dice.java:40) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)
Dice are used in many games. One die can be thrown to randomly show a value from 1 through 6.
Design a Die class that can hold an integer data field for a value (from1 to 6).
Include a constructor that randomly assigns a value to a die object. Appendix D contains information on generating randomnumbers. To fully understand the process, you must learn more about Java classes and methods. However, for now, you can copy the following statement to generate a random number between 1 and 6 and assign it to a variable. Using this statement assumes you have assigned appropriate values to the static constants.
Also include a method in the class to return a die's value. Save the class as
Die.java.
Write an application that randomly "throws" two dice and displays their values. After you read the chapter Making Decisions, you will be able to have the game determine the higher die. For now, just observe how the values change as you execute the program multiple times. Save the application as TwoDice.java.
The program is supposed to generate random dice rolls using the get and set methods. If the user doesn't specific how many sides the dice have, then it defaults at 6 with the constructor. The program runs but its generating 0's.
import java.util.Scanner; public class RollOfTheDice { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Time to roll the dice, how many sides do they have?"); int numSides = input.nextInt();