Basically, two dice are rolled randomly a bunch of times and then the two dice values are added together (You roll a 6 and a 3, your total is 9).
The frequency of how many times the total is rolled is stored in totalTally[].
So say you roll 10 times, 3 of those times you roll a total of 9, totalTally[9] = 3.
Here's my code:
import java.util.Random;
public class dice
{
public static void main(String a[])
{
System.out.println("Creating arrays to store information. This might take a while.");
int[][] tally = new int[6][6]; //Creates an array called tally to keep track of how many times each number was rolled. [Dice1][Dice2]
int[] totalTally = new int[12]; //Creates an array called totalTally to keep track of frequency of totals rolled.
int[][] roll = new int[36000000][2]; //Creates an array to store dice roll info.
System.out.println("Rolling two 6-sided dice " + roll.length + " times...");
Random r = new Random(); //Creates a new random number generator
for (int i = 0; i < roll.length; i++) //For loop that rolls the dice to fill the length of the array.
{
roll[i][0] = (r.nextInt(6) + 1); //Assigns random number between 1 and 6 to the array for dice 1 result.
roll[i][1] = (r.nextInt(6) + 1); //Assigns random number between 1 and 6 to the array for dice 2 result.
tally[roll[i][0]-1][roll[i][1]-1]++; //Increments by 1 the respective result in the tally array.
totalTally[roll[i][0] + roll[i][1]-2]++; //Increments by 1 the respective result in the totalTally array.
}
System.out.println("All done. Results are below.");
//Following lines print first table header
System.out.println("\n ROLL SUMMARY:");
System.out.println("Dice 1 + Dice 2 Frequency");
System.out.println("---------------------------------");
for (int i = 1; i <= totalTally.length; i++)//for loop goes through totalTally values
{
System.out.println(" " + i + " " + totalTally[i-1]);
}
//Following lines print second table header
System.out.println("\n DETAILED VIEW:");
System.out.println("Dice 1 Dice 2 Total Frequency");
System.out.println("---------------------------------------------");
for (int j = 1; j <= 6; j++) //For loop goes through dice 1 values
{
for (int k = 1; k <= 6; k++) // Nested for loop goes through dice 2 values
{
System.out.println(j + " " + k + " " + (j+k) + " " + tally[j-1][k-1]); //Prints line for table with dice values
}
}
}
}
and here is the output I am getting for the first table with that code:
Dice 1 + Dice 2 Frequency
1 998639
2 1997209
3 2998118
4 4000336
5 4999210
6 6001277
7 5001144
8 4000794
9 3002596
10 2001501
11 999176
12 0
Here's my issue: It's not possible to roll a 1 if you are rolling two dice.
And it IS possible to roll a 12.
So all my values need to be shifted.
What am I doing wrong?
totalTally[roll[i][0] + roll[i][1]-2]++
Why you subtract 2 ?
you should subtract 1 instead
totalTally[roll[i][0] + roll[i][1]-1]++
This is how you "tally" a single die.
Random r = new Random();
int[] die1 = new int[5]; //Creates an array to tally die 1
for (int roll = 0; roll < totalRolls; roll++) {
die1[r.nextInt(6)] += 1; // increment the index of the random roll
}
Do that for each die.
Your total tally would look like this to count individual rolls
for (int side = 1; side <= 6; side++) {
System.out.printf("%d\t%d\n", side, die1[side-1] + die2[side-1]);
}
If you want to tally the total of the rolls, sure, start printing at 2, not 1
System.out.println("Dice 1 + Dice 2 Frequency");
System.out.println("---------------------------------");
/// See here
for (int i = 1; i <= totalTally.length; i++)//for loop goes through totalTally values
{
System.out.println(" " + i + " " + totalTally[i-1]);
}
Related
I'm making a game for an extra assignment for my college and I have to create a dice game known as "balut" I'm having some issues with assigning values to an array, and having the dice values stored within this array.
I'm in week 9 of 11 of my course we've covered arrays and methods however this is a new concept for me. The goal is as follows:
Balut = all five dice have the same number.
Straight = a total of 15 Or 20.
Sixes = 1 or more sixes.
Fives = 1 or more fives.
Fours = 1 or more fours.
10 rounds.
Total scoring of categories.
total of scores.
If no category is met "none" is printed.
I've put at least 14 hours into this and it was intended to be a 6 to 8 hour program and I still am struggling, questioning if I have the intelligence for the course and am hoping someone here can explain what I'm doing wrong or even what I should be studying.
I've tried creating a single array and assigning all dice values to this array, I run into the problem of when it comes to comparing the values I don't know how to do dice 1 == dice 2 == dice 3 etc.
I've then attempted to make 5 arrays 1 for each dice and use the compare array method which again I can only seem to get it to compare 2 arrays or variables I can't get it to compare all 5 like I'm attempting.
public static void main(String[] args) {
int[] Dicearraytotal1 = new int[10];
int[] Dicearraytotal2 = new int[10];
int[] Dicearraytotal3 = new int[10];
int[] Dicearraytotal4 = new int[10];
int[] Dicearraytotal5 = new int[10];
for (int i = 0; i < Dicearraytotal1.length; i++) {
Integer dice1 = (int) (Math.random() * 6 + 1);
Integer dice1val = dice1;
Integer dice2 = (int) (Math.random() * 6 + 1);
Integer dice2val = dice2;
Integer dice3 = (int) (Math.random() * 6 + 1);
Integer dice3val = dice3;
Integer dice4 = (int) (Math.random() * 6 + 1);
Integer dice4val = dice4;
Integer dice5 = (int) (Math.random() * 6 + 1);
Integer dice5val = dice5;
Dicearraytotal1[i] = (dice1val);
Dicearraytotal2[i] = (dice2val);
Dicearraytotal3[i] = (dice3val);
Dicearraytotal4[i] = (dice4val);
Dicearraytotal5[i] = (dice5val);
Integer total = (dice1val+dice2val+dice3val+dice4val+dice5val);
System.out.println("Total Of Numbers Generated1: " + Arrays.toString(Dicearraytotal1));
System.out.println("Total Of Numbers Generated2: " + Arrays.toString(Dicearraytotal2));
System.out.println("Total Of Numbers Generated3: " + Arrays.toString(Dicearraytotal3));
System.out.println("Total Of Numbers Generated4: " + Arrays.toString(Dicearraytotal4));
System.out.println("Total Of Numbers Generated5: " + Arrays.toString(Dicearraytotal5));
System.out.println("Total: " + total);
You are missing an end bracket to stop the for loop, so I assume you were attempting to print the arrays each iteration of the loop. I cleaned up your code a lot and you should take note on some of the changes in order to organize your code better which will make it easier to understand.
public static void main(String[] args) {
int[] diceArrayTotal1 = new int[10];
int[] diceArrayTotal2 = new int[10];
int[] diceArrayTotal3 = new int[10];
int[] diceArrayTotal4 = new int[10];
int[] diceArrayTotal5 = new int[10];
int[] totals = new int[10];
for (int i = 0; i < diceArrayTotal1.length; i++) {
diceArrayTotal1[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal2[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal3[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal4[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal5[i] = (int) (Math.random() * 6 + 1);
totals[i] = (diceArrayTotal1[i] + diceArrayTotal2[i] + diceArrayTotal3[i] + diceArrayTotal4[i] + diceArrayTotal5[i]);
}
System.out.println("Total Of Numbers Generated1: " + Arrays.toString(diceArrayTotal1));
System.out.println("Total Of Numbers Generated2: " + Arrays.toString(diceArrayTotal2));
System.out.println("Total Of Numbers Generated3: " + Arrays.toString(diceArrayTotal3));
System.out.println("Total Of Numbers Generated4: " + Arrays.toString(diceArrayTotal4));
System.out.println("Total Of Numbers Generated5: " + Arrays.toString(diceArrayTotal5));
System.out.println("Totals: " + Arrays.toString(totals));
}
Additionally I added a totals array that keeps the total for each index instead of printing it every loop like you were doing. You did not add your compare code, so I cannot assist you with that. Let me know if you need any clarification on any changes. I ran this code and it successfully generated the arrays you need and the totals.
public static void main(String[] args) {
int[] results = new int[6]; // This array will hold the number of time each dice was rolled, so for example results[0] is how many 1 s you have results[5] is how many 6 you have
Random random = new Random();
for (int i = 0; i < 5; i++) { // roll the dice 5 times
results[random.nextInt(6)]++; //increase the counter of the appropriate value
}
boolean balut = false;
for (int i = 0; i < 6; i++) {
System.out.println("Number of " + (i+1) + ": " + results[i]);
if (results[i] == 5) {
balut = true;
}
}
if (balut) {
System.out.println("Balut!");
}
}
Here I implemented only the check for the Balut, but the main point is how I am couting the dices results. Hope it helps.
The goal of the assignment is to use parallel 1-D arrays, but 2-D arrays are also allowed.
I can print out the different combinations such as 1,1 (otherwise known as snake eyes) rolled by the pair of dice.
Trying to print out the number of times each combination was rolled without printing the combination the same number of times it was rolled is difficult.
Ex:
Enter the number of times you want to roll a pair of dice:
5
You rolled: 1 and 5 a total of 1 times - What I don't want
You rolled: 4 and 3 a total of 1 times
You rolled: 1 and 5 a total of 2 times - For duplicates this is all I want to print
You rolled: 3 and 3 a total of 1 times
You rolled: 2 and 2 a total of 1 times
I know the loop for printing it out right after it increments the combo array (which holds the number of times each combination was rolled) is not correct, but I am stuck on how to modify it.
I consider combo[0][0] to be the number of times 1,1 is rolled, combo[0][1] to be the number of times 1,2 is rolled, and so on.
import java.util.Scanner;
public class Dice {
Scanner read = new Scanner(System.in);
Random diceRoll = new Random();
int numRolls;
int[] dice1 = new int [1000];
int[] dice2 = new int [1000];
int[][] combo = new int[6][6];
public void getRolls()
{
System.out.println("Enter the number of times you want to roll a pair of dice: ");
numRolls = read.nextInt();
dice1 = new int[numRolls];
dice2 = new int[numRolls];
for (int i = 0; i < dice1.length; i++)
{
dice1[i] = diceRoll.nextInt(6) + 1;
dice2[i] = diceRoll.nextInt(6) + 1;
}
System.out.println("\n");
for (int j = 0; j < combo.length; j++)
{
for (int k = 0; k < combo[0].length; k++)
{
combo[j][k] = 0;
}
}
for (int m = 0; m < numRolls; m++)
{
combo[dice1[m] - 1][dice2[m] - 1]++;
System.out.println("You rolled: " + dice1[m] + " and " +
dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] +
" times");
}
You should separate the combo calculation loop from the printing loop. If the order is not relevant as you stated, that should give you the correct output you’re looking for. Happy coding!
Self-answer:
I made the printing loop separate from the combo calculation loop.
If the combo value for the combination is 1, then I simply print it out stating it was rolled 1 time.
If the combo value for the combination was greater than 1, I print it out at the first occurrence stating it was rolled that many times, then set the combo value for that combination equal to 0. Only combos with at least a combo value of 1 are printed, so duplicate lines cannot be printed (i.e. 1,1 rolled 4 times only prints on one line now instead of 4 separate lines).
for (int m = 0; m < numRolls; m++)
{
combo[dice1[m] - 1][dice2[m] - 1]++;
}
for (int m = 0; m < numRolls; m++)
{
if (combo[dice1[m] - 1][dice2[m] - 1] > 1)
{
System.out.println("You rolled: " + dice1[m] + " and " + dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] + " time(s)");
combo[dice1[m] - 1][dice2[m] - 1] = 0;
}
if (combo[dice1[m] - 1][dice2[m] - 1] == 1)
{
System.out.println("You rolled: " + dice1[m] + " and " + dice2[m] + " a total of " + combo[dice1[m] - 1][dice2[m] - 1] + " time(s)");
}
}
I am working on this question and I can not figure out how to get an array to keep track of the rolls in main and how to loop call the method that is doing the rolls. (Please help I am working on this by myself no teacher.)
The Problem:
Write a program that simulates rolling two dice. Prompt the user to enter the number of dice rolls. Use a loop to repeatedly call a method that simulates a dice roll and returns the total of the two dice to main. Keep track of the rolls in an array in main, and end the program by showing the results of the rolls.
Sample Output:
How many times should I roll the dice? 100
Results for 100 dice rolls
2 was rolled 4 times
3 was rolled 2 times
4 was rolled 1 times
5 was rolled 8 times
6 was rolled 15 times
7 was rolled 16 times
8 was rolled 17 times
9 was rolled 18 times
10 was rolled 10 times
11 was rolled 6 times
12 was rolled 3 times
My code so far:
import java.util.Scanner;
public class TestingCenter {
private static Scanner input;
public static void main(String[] args){
System.out.println("How many times should I roll the dice? ");
int answer = input.nextInt();
for (int x = 0; x < answer; x++) {
}
}
public static int amount(int x){
int die1;
int die2;
int roll;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;
return roll;
}
}
This is implementation with int[] in main method :
import java.util.Random;
import java.util.Scanner;
public class TestingCenter {
private static final Random random = new Random();
public static void main(final String[] args) {
System.out.println("How many times should I roll the dice? ");
int answer = 0;
try (Scanner scanner = new Scanner(System.in)) {
answer = scanner.nextInt();
}
final int[] results = new int[11];
for (int x = 0; x < answer; x++) {
results[amount() - 2]++;
}
System.out.println(String.format("Results for %s dice rolls ", answer));
for (int i = 0; i < 11; i++) {
System.out.println(String.format("%s was rolled %s times", i + 2, results[i]));
}
}
public static int amount() {
return random.nextInt(6) + random.nextInt(6) + 2;
}
}
Tested output:
How many times should I roll the dice?
100
Results for 100 dice rolls
2 was rolled 1 times
3 was rolled 8 times
4 was rolled 10 times
5 was rolled 12 times
6 was rolled 14 times
7 was rolled 18 times
8 was rolled 13 times
9 was rolled 12 times
10 was rolled 6 times
11 was rolled 4 times
12 was rolled 2 times
You may try something like this (Explanation in Comments):
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TestingCenter {
// map to hold the results
static Map<Integer, Integer> result = new HashMap<>();
public static void main(String[] args){
// fill the map with numbers from 1 to 12 like the die
// each number has a corresponding value, initially 0
for(int i=1; i<=12; i++){
result.put(i, 0);
}
// initialize the Scanner
Scanner input = new Scanner(System.in);
System.out.print("How many times should I roll the dice? ");
int answer = input.nextInt();
// repeat the rolling
for (int x = 0; x < answer; x++) {
rollDice();
}
input.close();
// final result
System.out.println("Results for " + answer + " dice rolls:");
for(Integer die : result.keySet()){
System.out.println(die + " was rolled " + result.get(die) + " times");
}
}
// this method returns the roll result
public static void rollDice(){
int die1, die2, roll;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;
// increment the current value and update the map result
result.put(roll, result.get(roll)+1);
}
}
Test
How many times should I roll the dice? 100
Results for 100 dice rolls:
1 was rolled 0 times
2 was rolled 5 times
3 was rolled 1 times
4 was rolled 8 times
5 was rolled 17 times
6 was rolled 12 times
7 was rolled 22 times
8 was rolled 8 times
9 was rolled 10 times
10 was rolled 7 times
11 was rolled 9 times
12 was rolled 1 times
import java.util.Arraylist and simply add each value to the List as your looping answer number of times.
public static void main(String[] args)
{
input = new Scanner(System.in);
System.out.println("How many times should I roll the dice? ");
int answer = input.nextInt();
List<Integer> list = new ArrayList<Integer>();
for (int x = 0; x < answer; x++)
{
list.add(amount(x));
}
}
What you could do is create a HashMap, with a key containing the result of the roll, and the second value being the number of times this result was rolled out. It would give something like this :
HashMap<Integer, Integer> results = new HashMap<Integer, Integer>();
for (int x = 0; x < answer; x++) {
int roll = amount(x);
if(results.get(roll) == null) {
results.put(roll, 1);
} else {
results.put(roll, results.get(roll) +1);
}
}
Then, when printing, you just have to do a for loop (from 1 to 12) and then get the value associated to every number looped.
I wrote a programm to print out all the prime number up to a limit. Then put those prime numbers into an ArrayList, convert this ArrayList into an array, finally print the numbers out with their index.
Note: I couldnt print out an ArrayList with a for loop, it's possible with an array only, hence the conversion.
My question is:
Is there anyway to do it without pushing the numbers into an ArrayList first, instead put them right away into an emty array of a length < given limit.
Thanks for you help!
was browsing SO in the last 2 days and couldnt find anything
import java.util.Scanner;
import java.util.ArrayList;
public class test {
public static void main(String[] args){
//get input till which prime number to be printed
System.out.print("Enter the number till which prime number to be printed: ");
int limit = new Scanner(System.in).nextInt();
System.out.print("\n");
ArrayList<Integer> myArray = new ArrayList<Integer>();
//printing primer numbers till the limit ( 1 to 10)
System.out.println("Printing prime number from 1 to " + limit + "\n");
for(int number=2; number <= limit; number++){
if(isPrime.numberPrime(number)){
myArray.add(number);
}
}
System.out.println(myArray + "\n");
//Convert ArrayList into an Array
int[] newArray = new int[myArray.size()];
for(int i = 0; i < myArray.size(); i++) {
newArray[i] = myArray.get(i);
}
System.out.println("There were " + myArray.size() + " prime numbers");
System.out.println("Index\tValue");
for(int counter = 0; counter<newArray.length; counter++){
System.out.println(counter+1 + "\t" + newArray[counter]);
}
}
here is the result:
Enter the number till which prime number to be printed: 10
Printing prime number from 1 to 10
[2, 3, 5, 7]
There were 4 prime numbers
Index Value
1 2
2 3
3 5
4 7
No need to do this
//Convert ArrayList into an Array
int[] newArray = new int[myArray.size()];
for(int i = 0; i < myArray.size(); i++) {
newArray[i] = myArray.get(i);
}
System.out.println("There were " + myArray.size() + " prime numbers");
System.out.println("Index\tValue");
for(int counter = 0; counter<newArray.length; counter++){
System.out.println(counter+1 + "\t" + newArray[counter]);
}
In place of this, simply write
System.out.println("There were " + myArray.size() + " prime numbers");
System.out.println("Index\tValue");
for(int i=0;i<myArray.size();i++)
System.out.println(i+1 + "\t" + myArray.get(i));
This is part of a school assignment which I've already turned in, I was graded down in one portion because the total sum of the random numbers was supposed to be adding the individual numbers together, not each line.
So if the lines read:
1 2 3
4 5 6
The total should be 21, not 579 as my program is doing now. I've really been struggling trying to figure this out. I tried generating a different random number object for each integer, for a total of three of them but that completely screwed up my output.
We're learning arrays next week, and after researching online I could do this easily with an array, but the assignment was to be done without arrays. How can I sum each entry individually? Thanks for any help!
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class lottery {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int gameType, gameTimes;
int randomNums, lotNum, sum = 0;
System.out.println("\t\t Welcome to\n \t\tJAVA LOTTERY!\n\n\n");
System.out.print(" Would you like to play with 3, 4, or 5 numbers? ");
gameType = input.nextInt();
System.out.println("\n\n Now think about a number with " + gameType + " digits and remember it!\n\n\n");
System.out.print(" How many games should we play? ");
gameTimes = input.nextInt();
System.out.println("\n\n We're all done! We played " + gameTimes + " games!\n\n" +
" The numbers randomly selected were: ");
for(int i = 0; i < gameTimes; i++)
{
lotNum = 0;
for(int j = 0; j < gameType; j++)
{
randomNums = (new Random()).nextInt(10);
lotNum = (lotNum * 10) + randomNums;
System.out.print(" " + randomNums);
}//end nested for loop
System.out.println();
sum += lotNum;
}//end for loop
System.out.println("\n\nThe total of all of the numbers was " + sum );
input.close();
}//end main method
}//end lottery class
Change
lotNum = (lotNum * 10) + randomNums;
to
lotNum += randomNums;