-2

我正在编写一个掷 2 个骰子的程序,并为用户提供重复掷骰的选项(继续 y/n)。此外,应用程序需要识别何时进行某些滚动,例如滚动 7 或 2(蛇眼),我不确定是否需要进入主方法或有自己的类,我迷路了!请帮忙。以下是我对应用程序需要的大纲的概述,我不确定如何对其进行编码。

这是我第一次编写自己的类以在 main 方法中使用,布局如下:

public class DiceRollerApp
{
    public static void main(String[] args)
    {
    }// end main method
}// end App class

class Die
{
    public Die()
    {}           // default to six-sided die
    public Die(int sides)
    {}  // variable number of sides
    public void roll()
    {}     // randomly picks a face value
    public int getValue()
    {}  // returns the face value
}// end class Die

class PairOfDice
{
    public PairOfDice()
    {}          // default to six-sided dice
    public PairOfDice(int sides)
    {} // variable number of sides
    public void roll()
    {}         // roll both dice
    public int getValue1(){}       // get value of die1
    public int getValue2(){}       // get value of die2
    public int getSum()    {}
    // get sum of both dice
}// end class PairOfDice


public class Validator
{
    public static String getString(Scanner sc, String prompt)
    {
        System.out.print(prompt);
        String s = sc.next();  // read user entry
        sc.nextLine();  // discard any other data entered on the line
        return s;
    }

    public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    public static int getInt(Scanner sc, String prompt,
    int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
            System.out.println(
            "Error! Number must be greater than " + min + ".");
            else if (i >= max)
            System.out.println(
            "Error! Number must be less than " + max + ".");
            else
            isValid = true;
        }
        return i;
    }

    public static double getDouble(Scanner sc, String prompt)
    {
        double d = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return d;
    }

    public static double getDouble(Scanner sc, String prompt,
    double min, double max)
    {
        double d = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            d = getDouble(sc, prompt);
            if (d <= min)
            System.out.println(
            "Error! Number must be greater than " + min + ".");
            else if (d >= max)
            System.out.println(
            "Error! Number must be less than " + max + ".");
            else
            isValid = true;
        }
        return d;
    }
} // end class Validator

我已经编写了验证器类,但没有在此处发布,其目的是简单地验证问题“继续?y/n:”中的字符串

我不知道如何编写骰子类和一对骰子类,我不知道我是否需要为 DiceRollerApp 设置一个单独的类。

4

1 回答 1

0
class Die 
{
    private int sides;

    private int value;

    public Die() {
        this.sides = 6;
    }           // default to six-sided die

    public Die(int sides) {
        this.sides = sides;
    }  // variable number of sides

    public void roll() {
        value = (int)(Math.random() * (sides-1)) + 1;
    }     // randomly picks a face value

    public int getValue() {
        return value;
    }  // returns the face value

}// end class Die
于 2013-02-09T00:03:12.327 回答