Я пытаюсь создать программу, которая попросит пользователя ввести число 6-сторонних кубиков, которые будут свернуты, а затем сколько рулонов (проб) они хотят запустить. В конце кода он должен отображать пользователю, сколько из них может быть сколько угодно, и если бы была полоса (если бы это было, то нужно было сказать, что такое полоса, и по какому номеру ее начала.Dice Roller Simulator с использованием массивов в java
To прост, до сих пор моя программа должна только спросить, сколько проб будет выполняться с двумя 6-сторонними кубиками и показать дисплеи, как указано выше.
Проблема заключается в том, что на дисплее не отображаются проценты, которые должна позволять моя математика и когда он приходит к той полосе, о которой я не знаю, как заявить, в каком испытании она началась, и о том, какое количество было на полосе .... Любая помощь будет принята с благодарностью.
import java.util.*;
общественного класса RollThoseDice {
public static void main(String[] args) {
//start of method
//variables needed for program
int total;
int newStreak = 1;
int streak = 1;
int totalIs2 = 0;
int totalIs3 = 0;
int totalIs4 = 0;
int totalIs5 = 0;
int totalIs6 = 0;
int totalIs7 = 0;
int totalIs8 = 0;
int totalIs9 = 0;
int totalIs10 = 0;
int totalIs11 = 0;
int totalIs12 = 0;
double twoPercent = 0;
double threePercent = 0;
double fourPercent = 0;
double fivePercent = 0;
double sixPercent = 0;
double sevenPercent = 0;
double eightPercent = 0;
double ninePercent = 0;
double tenPercent = 0;
double elevenPercent = 0;
double twelvePercent = 0;
//intro to program and purpose
System.out.println("Today we are going to generate 2 random dice and tally the results of their random combined rolls");
System.out.println("At the bottom of the results, the longest streak will also be listed");
//variable for while loop
boolean validInput = true;
//declaration of scanner before try/catch
Scanner userInput = new Scanner(System.in);
//test for valid input
while (validInput){
try {
System.out.print(" \n" + "Please enter the number of trials you would like to be performed:");
int numberOfRolls = Integer.parseInt(userInput.nextLine());
//Stop from calling for anything else
userInput.close();
//declaring of variables for die 1 and die 2
int die1[] = new int[numberOfRolls];
int die2[] = new int[numberOfRolls];
//create an array for each die roll so that they can each be saved for recalling streak
int[] array = new int[numberOfRolls];
for (int i=1; i<array.length; i++) {
die1[i] = (int) ((Math.random() * 6) + 1);
die2[i] = (int) ((Math.random() * 6) + 1);
total = die1[i] + die2[i];
//streak checker
int lastTotal = die1[i-1] + die2[i-1];
if (lastTotal == total) {
streak++;
if (streak > newStreak) {
newStreak = streak;
}
} else {
streak = 1;
}
//count total of each numbered possibility rolled
if (total == 2) {
totalIs2++;
}
if (total == 3) {
totalIs3++;
}
if (total == 4) {
totalIs4++;
}
if (total == 5) {
totalIs5++;
}
if (total == 6) {
totalIs6++;
}
if (total == 7) {
totalIs7++;
}
if (total == 8) {
totalIs8++;
}
if (total == 9) {
totalIs9++;
}
if (total == 10) {
totalIs10++;
}
if (total == 11) {
totalIs11++;
}
if (total == 12) {
totalIs12++;
//calculate percent of each number rolled
twoPercent = (totalIs2/numberOfRolls);
threePercent = (totalIs3/numberOfRolls);
fourPercent = (totalIs4/numberOfRolls);
fivePercent = (totalIs5/numberOfRolls);
sixPercent = (totalIs6/numberOfRolls);
sevenPercent = (totalIs7/numberOfRolls);
eightPercent = (totalIs8/numberOfRolls);
ninePercent = (totalIs9/numberOfRolls);
tenPercent = (totalIs10/numberOfRolls);
elevenPercent = (totalIs11/numberOfRolls);
twelvePercent = (totalIs12/numberOfRolls);
}
}
//results
System.out.println("\n" + "Total Results:");
System.out.println("\n" + "Total 2 happened " + totalIs2 + " times which is " + twoPercent + "%");
System.out.println("Total 3 happened " + totalIs3 + " times which is " + threePercent + "%");
System.out.println("Total 4 happened " + totalIs4 + " times which is " + fourPercent + "%");
System.out.println("Total 5 happened " + totalIs5 + " times which is " + fivePercent + "%");
System.out.println("Total 6 happened " + totalIs6 + " times which is " + sixPercent + "%");
System.out.println("Total 7 happened " + totalIs7 + " times which is " + sevenPercent + "%");
System.out.println("Total 8 happened " + totalIs8 + " times which is " + eightPercent + "%");
System.out.println("Total 9 happened " + totalIs9 + " times which is " + ninePercent + "%");
System.out.println("Total 10 happened " + totalIs10 + " times which is " + tenPercent + "%");
System.out.println("Total 11 happened " + totalIs11 + " times which is " + elevenPercent + "%");
System.out.println("Total 12 happened " + totalIs12 + " times which is " + twelvePercent + "%");
System.out.println("The longest run was a run of " + newStreak + " *number that was on a streak*" + " that began at roll" + "*where it started*");
//stop the loop
validInput = false;
}
//catch exception and call for new input
catch(Exception e){
System.out.println("\n" + "Your input was not a number. Please try again: ");
}
}
}
Почему не ваш цикл, начиная с 0? Вы должны сохранить общую сумму двух матриц в массиве «array» –
, когда у вас есть длинная строка операторов IF, подобных этому, вы должны рассмотреть возможность использования оператора switch для ясности. –