0
有人可以幫我弄清楚如何讓這個程序顯示最長的連勝。我擲兩個骰子,並記錄每個骰子總數的百分比。現在我需要一些能夠告訴我什麼是最長連勝的骰子,例如「最長的跑步是從1479966捲開始的8 7的跑步。」RollingDice Java程序最長連勝
import java.util.Scanner;
import java.text.DecimalFormat;
public class RollThoseDice {
/* Author: Name
* Assignment: Third Program
*/
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int timesRolled, randomOutSum;
DecimalFormat df = new DecimalFormat ("#.###");
System.out.println("Name: "
+ "\nAssignment: Third Program"
+"\nExtra Credit: Percentage is displayed in three decimal places");
int[] d = new int[13];
for (int i = 2; i < 13; i++) d[i] = 0;
int N=0;
boolean againA = true;
while(againA) {
try{
System.out.print("\nHow Many Rolls? ");
N =kbd.nextInt();
againA = false;
} catch(Exception e) {
System.out.println("Invalid Input");
kbd.next();
}
}
for (int k = 0; k < N; k++) {
int diceOut1 = (int) (Math.random()*6+1);
int diceOut2 = (int) (Math.random()*6+1);
int diceSum = diceOut1 + diceOut2;
d[diceSum]++;
}
for (int i = 2; i < 13; i++)
System.out.println("Total " + i + " happened "
+ df.format((double) (d[(int) i]/(double) N) * 100)
+ "% of the time.");
}
}
你能告訴我們你到目前爲止所嘗試過的嗎? – doelleri 2015-02-06 19:21:49
似乎是[基本的Java Dice程序 - 找到最長連續的故障]的副本(http://stackoverflow.com/questions/28354928/basic-java-dice-program-trouble-finding-longest-streak)。 – doelleri 2015-02-06 20:18:26