2015-02-06 47 views
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."); 
    } 

} 
+0

你能告訴我們你到目前爲止所嘗試過的嗎? – doelleri 2015-02-06 19:21:49

+0

似乎是[基本的Java Dice程序 - 找到最長連續的故障]的副本(http://stackoverflow.com/questions/28354928/basic-java-dice-program-trouble-finding-longest-streak)。 – doelleri 2015-02-06 20:18:26

回答

0

最長的運行是7/8的運行開始時間爲卷1479966.

那麼,什麼是在輸出參數?

看起來像滾動,運行長度或滾動計數和滾動編號。

嗯,什麼可以保持多個變量,所以我們可以把它們當作一件事?

我知道,我們將創建一個模型類

public class RollStreak { 

    private final NumberFormat NF = NumberFormat.getInstance(); 

    private int roll; 
    private int rollCount; 

    private long rollNumber; 

    public RollStreak(int roll, long rollNumber, int rollCount) { 
     this.roll = roll; 
     this.rollNumber = rollNumber; 
     this.rollCount = rollCount; 
    } 

    public int getRoll() { 
     return roll; 
    } 

    public int getRollCount() { 
     return rollCount; 
    } 

    public long getRollNumber() { 
     return rollNumber; 
    } 

    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("The computer rolled a "); 
     builder.append(roll); 
     builder.append(" "); 
     builder.append(rollCount); 
     builder.append(" times in a row starting at roll number "); 
     builder.append(NF.format(rollNumber)); 
     builder.append("."); 

     return builder.toString(); 
    } 

} 

模型類允許您將一組字段視爲一個對象。

我不想放棄整個答案。您仍然必須確定如何創建這些模型類實例並獲得最長的條紋。提示:可以有不止一個最長的連勝。