At the moment I am learning Java, and I am currently going through pretty much basics. I have an assignment that makes me practice for loops and random numbers. The assignment tells 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.
I'm supposed to ask the user how many times to roll the dice, and from there calculate the probability of the sum to come up. For now, I'm doing it with a 4 sided dice. What I'm having a problem with is making the loops calculate the probability for the sums.
This is what I have:
Code:
int rolls;
int counter = 0;
int dice1 = 0;
int dice2 = 0;
Scanner in = new Scanner(System.in);
System.out.print("Number of rolls: ");
rolls = in.nextInt();
Random randNumList = new Random();
timesRolled = 0;
System.out.println();
System.out.println("Times rolled: " + timesRolled);
System.out.println("Sum of dice Probability");
for(timesRolled = 0; rolls > timesRolled; timesRolled++)
{
dice1 = randNumList.nextInt(5);
dice2 = randNumList.nextInt(5);
for(int diceSum = (dice1 + dice2); diceSum == 2; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 2s: " + prob);
}
for(int diceSum = dice1 + dice2; diceSum == 3; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 3s: " + prob);
}
for(int diceSum = dice1 + dice2; diceSum == 4; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 4s: " + prob);
}
for(int diceSum = dice1 + dice2; diceSum == 5; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 5s: " + prob);
}
for(int diceSum = dice1 + dice2; diceSum == 6; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 6s: " + prob);
}
for(int diceSum = dice1 + dice2; diceSum == 7; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 7s: " + prob);
}
for(int diceSum = dice1 + dice2; diceSum == 8; counter++)
{
double prob = (double)counter / 8;
System.out.println(" 8s: " + prob);
}
}
As you can see, it's very basic printing-to-terminal kind of stuff. When I run this, it doesn't work properly. Any help is appreciated. I've asked my teacher for help but all he does is give me hints, and its frustrating because I'm good with methods, but not with the math part.
Thanks