DiceRoll program using arrays, how can I accommodate multiple players? - java

I am trying to make a program that takes an input for #players and #dice and rolls the dice whatever number of times for each player. It then outputs the rolls and the total.
So far I've managed to develop a program that can roll as many dice as inputted and store these values in an array, which it then sums and outputs.
Unfortunately, I'm stuck as of now because I don't really have any clue what to do when trying to make the program do this again each time for a new player. I understand it would probably be with an incrementer, but I'm just really overwhelmed by the complexity and don't even know what I would look for online.
Here is my code:
package diceroll;
import java.util.ArrayList;
import java.util.Scanner;
public class DiceRoll {
public static void main(String[] args) {
int numplayers = 0, numdice = 0; // incrementers for #rolls and #players
// ArrayList<ArrayList> players = new ArrayList<ArrayList>();
// players.add(rolls); /// adding list to a list
// System.out.println(players);
ArrayList<Integer> rolls = new ArrayList<>();
System.out.println("Enter the number of players.");
Scanner scan = new Scanner (System.in);
numplayers = scan.nextInt();
System.out.println("Enter the number of dice.");
numdice = scan.nextInt();
while (numdice > 0 ) {
Die die1 = new Die();
die1.roll();
rolls.add(die1.getFaceValue());
numdice--;}
System.out.println(rolls);
// sum for array, but i cant access the arraylength
int total = 0;
for (int n : rolls) //what does the colon : do ?
{total += n;
System.out.println("Dice total:" + total);
}
}
}
There is also a basic Die.java class that assigns a random number to face value and has the roll method I use to randomize the die.
Output:
run:
Enter the number of players.
1
Enter the number of dice.
4
[5, 4, 6, 6]
5
9
15
The only problem is changing the number of players currently has no effect.
21

You may probably want to use another loop outside your while loop, if you want to repeat the #dice for all the players.
The for(int i:rolls) --> this statement is read as "for each integer 'i' in rolls" which means, for every iteration of the loop, the values in rolls are assigned to y:
And this is equivalent to
for(int j=0;j<rolls.size();j++){
i = rolls[j];
// Other statements goes here.
}

Related

Modification of Bubblesort program with user input

I have created a program previously using the BubbleSort method that works to sort numbers in a list that already exists, however, I am having difficulty with trying to manipulate this program in order to allow a user to input the list of numbers to be sorted instead. So far I have:
import java.util.Scanner;
public class MedianValue {
public static void main(String[] args) {
//use scanner to input list of numbers to sort
Scanner scan = new Scanner(System.in);
int[] numbers = new int[] {scan.nextInt()};
//nested for loop
//outer loop just iterating
//inner loop going through and flipping
//checking if out of order (if statement)
int counter = 0;
//outer loop: keep doing this until it's sorted
for(int i = 0; i < numbers.length - 1; i = i + 1)
//put in a inner loop number.length times minus one because we don't want to swap the last element
for(counter = 0; counter < numbers.length - 1; counter = counter + 1)
{
if (numbers [counter] > numbers [counter + 1])
{
int temporary = numbers [counter];
numbers [counter] = numbers [counter + 1];
numbers [counter + 1] = temporary;
}
}
for(int i =0; i < numbers.length; i = i + 1)
{
System.out.print(numbers[i] + " ");
}
}
}
But, in this program, instead of sorting the inputted numbers, the program simply prints the first number that is inputted by the user. I am not sure if I need to move where my scanner function is placed, or add on to it within the loop for it to sort all of the numbers as I want it to do. I am lost on where to change the program if that is the case.
That's because int[] numbers = new int[] {scan.nextInt()}; is a single assigment. scan read a single input and assign to number[0].
You actually need to modify your code for scan to read n numbers and store in n-sized numbers.
something like.
int[] numbers = new int[scan.nextInt()];
for( int i = 0; i < numbers.length; i++)
numbers[i] = scan.nextInt();
The code int[] numbers = new int[] {scan.nextInt()}; will always create an array (not a List) of size 1.
Usually in these kinds of assignments you get n + 1 numbers, for example 5 3 6 2 4 1 would mean "I'm going to give you five numbers. Oh here they are: 3 6 2 4 and 1!"
You probably want something like int[] numbers = new int[scan.nextInt()]; - then loop from 0 to numbers.length to fill the array.

Dice Roll Simulation in Java

i searched this site and found similar codes but a little different. For my lab in class the instructions say that I need to roll N die , M times and display the number of times a roll occurs in a frequency table. so I'm having a bit of trouble setting up the data in my frequency table. The frequency table prints right but my numbers are wrong. I feel like I'm missing something either and array or an increment? Please Help
my input:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Lab1
{
public static void main(String[] arg)
{
Random ran = new Random();
String n = JOptionPane.showInputDialog(null,"How many dice?: ");
String m = JOptionPane.showInputDialog(null,"How many rolls?: ");
int die = Integer.parseInt(n);
int roll = Integer.parseInt(m);
int count = 0;
int[] rollArr = new int[die*6+1];
for(int i=0;i<rollArr.length;i++)
{
rollArr[i] = 0;
}
for( int i=0; i<roll;i++)
{
count = die*1 + ran.nextInt ( 6 );
++rollArr[count];
}
for(int r=1;r<rollArr.length;r++)
System.out.println(r + ":" + rollArr[r]);
}
}
The example says to roll 4 dice 1,000,000 times to get a bell curve but my curve has alot of zeroes.
my output:
1:0
2:0
3:0
4:166309
5:166424
6:166523
7:166984
8:167286
9:166474
10:0
11:0
12:0
13:0
14:0
15:0
16:0
17:0
18:0
19:0
20:0
21:0
22:0
23:0
24:0
I'll just give a hint since this is a lab assignment.
I believe your instructor wants to see the distribution of sums of the die rolls, since that's what will show up as a bell curve. So your array would have to capture and count the possible sums. For example, if you rolled four dice, then the possible sums are 4-24, and you would tally those up.

Arrays: Create Output Table and Provide the Average

I need to create an array that allows the user to enter no more than 10 pieces of data (in this case number of shirts purchased). The user can enter -1 to finish if < 10 pieces of data or count will terminate at 10. For the output I need to create a Table that shows the user the # in one column and the # shirts purchased in the other, both need a header. I also need the average of the shirts purchased.
If the user enters 0 for shirts purchased I need the system to provide a statement but then continue with the loop.
Below is what I have so far. I am very new to this so any dumb downed explanations would be extremely helpful.
import java.util.Scanner;
public class Arrays
{
private static int number;
public static void main(String[] args)
{
final int ARRAY_SIZE = 9;
int[] shirtsPurchased = new int[ARRAY_SIZE];
int count = 0;
int sum = 0;
double average = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of shirts purchased or -1 to quit: ");
number = keyboard.nextInt();
while (number != -1 && count < ARRAY_SIZE)
{
shirtsPurchased[count] = number;
count++;
System.out.print("Enter the number of shirts purchased or -1 to quit: ");
number = keyboard.nextInt();
}
}
}
You have the lions share of what you need to do already done (I'm not familiar with Java, some syntax errors may be present, but I don't really see any). From what you've described, all I can see that you still have to do are the message if the number of shirts is zero, and the average calculation. While I won't give you the code, I'm sure you can find out how to go about it.
As far as the zero is concerned, you have the input, all you need to do is compare it to zero and output a message if necessary. A simple if statement would take care of that.
To calculate the average, all you'll need is another while loop to sum up all of the numbers of shirts purchased, and divide the result by the number of data elements. Something similar to...
while( count is less than number of total shirts ) sum += shirtsPurchased[count];
average = sum / count;

Method return Java assignment

Okay here's a Java assignment I've been having trouble with. I asked earlier about this and got some good comments and advice, but have since understood the assignment more clearly and the issue has changed a bit. So here's the assignment:
***
Your task is to complete the program below by writing three methods (askInfo, copyInfo and setArray). Program should ask for integers (max 100 integers) until the users types in zero. Integers can vary from one to one hundred and they are stored in an array that has 100 elements. Numbers are asked for with the askInfo method, which receives the array with numbers as parameter. Method returns the number of integers. The number zero is not saved in the array; it is merely used to stop giving input. The given numbers are then copied to another array which size is the amount of given numbers. Copying is done with copyInfo method which receives both arrays as parameters. After this the elements of the new array are put in ascending order with setArray method and printed on screen with printArray method.
Program to complete:
import java.util.*;
public class RevisionExercise {
public static void main(String[] args) {
int[] tempArray = new int[100];
System.out.println("Type in numbers. Type zero to quit.");
int amountOfNumbers = askInfo(tempArray);
int[] realArray = new int[amountOfNumbers];
copyInfo(realArray, tempArray);
setArray(realArray);
printArray(realArray);
}
// Your code here
public static void printArray(int[] realArray ) {
System.out.println("\Ordered array: ");
for(int i = 0; i < realArray .length; i++) {
System.out.println(realArray [i]);
}
}
Example print:
Type in numbers. Type zero to quit.
1. number: 3
2. number: 8
3. number: 5
4. number: 6
5. number: 9
6. number: 0
Ordered array:
9
8
6
5
3
I'm struggling with the askInfo method. So far I've written this but it returns only zeroes. Here's my askInfo method:
public static int askInfo(int[] tempArray) { //askinfo-metodi
Scanner reader = new Scanner(System.in);
int i;
for (i = 0; i < tempArray.length; i++) {
System.out.print((i+1) + ". number: ");
tempArray[i] = reader.nextInt();
if (tempArray[i] == 0) {
return tempArray[i];
}
}
return tempArray[i];
}
***
How can I make it to register the input and get the amount of numbers to be passed to the next method in the assignment as described in the assignment.
You never store your integer luku values in your array, so your array never changes from the default initialized integer values of all zeroes. Inside your loop, you need to add an
tempA[i] = luku;
After the if-statement confirms that luku is not 0. All in all:
if (luku == 0) {
return i;
}
tempA[i] = luku;

How to print out the number of times two six sided dice add to 4

My previous question was similar to this, except I did not mention my ending goal,
In this code I have one die, and I print out the number of times it rolls a 4. Now I want to know if I have two dice, both six sided, how many times both would roll 2 and thus add to 4.
However, I would need to use arrays because it is mandatory in this assignment. I have tried adding another exception in the if statement, except I keep realizing that I need to be actually using arrays in the program. 1000 arrays should be stored, since the dice has to roll 1000 times, and thus check how many times it added to 4 from the rolls, then print the amount of times.
import java.io.*;
public class dont {
public static void main(String[] args) throws Exception {
// System.out.println(input());
int[] counts = new int[13];
System.out.print("The number of times it rolls 4 on two 6 sided dice :" + counts);
}
public static int input () throws IOException {
BufferedReader myInput = new BufferedReader (new InputStreamReader
System.out.println("Hello and welcome to the program");
System.out.println("In this program two six sided dices will be rolled and one eleven
sided dice will be rolled (1000 times each");
int sum;
int[] counts = new int[13];
System.out.println("The dices will be rolled to determine the odds of how many times the roll 2 comes up on both dies(Press any key to con't) ");
myInput.readLine();
//int count2=0;
int Sixside;
for (int i = 0; i < 1000; i++)
{
// two dice that add to 4, after being rolled one thousand times
Sixside = (int)(Math.random ()*6+1)+(int)(Math.random ()*6+1) == 4;
//print the number of times they add to 4
counts[sum]++;
}
counts[i] = Sixside;
{
//return array to main
return counts [13];
}
}
}
Your example produces reasonable answers for two dice that sum to 4. I suspect you're supposed to create an array that can hold sums for any pair with a sum between 2 and 12, for example:
int[] counts = new int[13];
Then you don't need an if statement in your loop; you can just increment the count for that sum, for example:
counts[sum]++;
This table can help you decide if your other counts are reasonable.
Addendum: Here's a simplified version of your method:
public static int[] input() {
int[] counts = new int[13];
for (int i = 0; i < 1000; i++) {
int sum = // your expression for the sum of two dice
counts[sum]++;
}
return counts;
}
You can call it like this and examine any particular total, e.g. counts[4].
int[] counts = input();

Categories

Resources