RollingDice Java程序最长的条纹

时间:2015-02-06 19:18:03

标签: java dice

有人可以帮我弄清楚如何让这个程序显示最长的条纹。我正在掷两个骰子,并记录每个骰子总和的百分比。现在我需要一些能够告诉我最长连胜的骰子数量的东西,例如“最长的跑步是从7月7日开始以1479966开始。”

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.");
    }

}

1 个答案:

答案 0 :(得分:0)

  

最长的一次是从7月7日开始,从第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();
    }

}

模型类允许您将一组字段视为一个对象。

我不想放弃整个答案。您仍然需要确定如何创建这些模型类实例并获得最长的条纹。提示:可能有不止一条最长条纹。