骰子滚轮应用多类

时间:2013-02-08 23:54:02

标签: java class

我正在编写一个滚动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

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

我不知道如何编写die类和一对dice类,我不知道我是否需要为DiceRollerApp创建一个单独的类。

1 个答案:

答案 0 :(得分: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