我想創建一個程序,要求用戶輸入要滾動的6個雙面骰子的數量,然後輸入他們想要運行多少個滾動(試驗)。在代碼結束時,它應該向用戶顯示每個可能的數字有多少,並且是否存在條紋(如果有的話應該說明條紋是什麼以及它開始的滾動編號。)在Java中使用數組的骰子滾筒模擬器
保持簡單到目前爲止,我的程序應該只詢問用兩個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: ");
}
}
}
爲什麼不是你的for循環從0開始? 當你有很長的這樣的IF語句時,你應該把這兩個die的總和存儲在'array'數組中 – 2015-02-06 09:23:28
,你應該考慮使用switch語句來清晰。 – 2015-02-06 09:51:06