-5

三个骰子对象并各扔一个。投掷的结果应显示在输出窗口中。我正在做这个练习。我有两个类:要使用的AppDice类,仅此而已。

我不确定如何让它在 App 类中工作。如果我在 Class App 上的错误如下,请纠正我。

更新:

类 App 上有一个错误,没有读取变量,我错误地将“Dice”用作 Dice [1],它应该读作face.Value使用接受的答案如下“**骰子:”+ faceValue。作为以下代码的解决方案: //UPDATED CODE and CORRECTED oBox.println("You throw : " + diceOne.getFaceValue() + " " + diceTwo.getFaceValue() + " " + diceThree.getFaceValue() ); **

类应用程序如下

import javabook.*;

class App
{

public static void main(String args[])
{
    App thisProgram = new App();
    //Scanner input= new Scanner(System.in);

}   
    //outside a main class
    public App()
    {

        //contsructor
        //Dice aDice

        //set variables
        //int anDice = 0;
        //int faceValue;

        //Declare objects
        Dice diceOne;       
        Dice diceTwo;       
        Dice diceThree;

        int aNumber = 0;
        //int afaceValue;

        //declare objects
        MainWindow mWindow;
        Dice aDice;     
        InputBox iBox;
        OutputBox oBox;


        //create objects                
        mWindow = new MainWindow();     //swap the words around e.g. MainWindow mWindow; to mWindow = new MainWindow();
        aDice = new Dice();             //aTriangle = new Triangle();
        iBox = new InputBox(mWindow);   //mWindow is a white screen behind the Input Box
        oBox = new OutputBox(mWindow);  //mWindow is a white screen behind the Input Box

        diceOne     = new Dice();
    diceTwo     = new Dice();
    diceThree   = new Dice();

        //Use objects
        mWindow.show();
        oBox.show();

        //LOOP
        do
        {
            //Get Input
            aNumber = iBox.getInteger("Enter 1 to throw the dice, or 0 to exit: ");
            //Process
            diceOne.throwDice();
            diceTwo.throwDice();
            diceThree.throwDice();
            //Output
                    //UPDATED and CORRECTED
            oBox.println( "You threw : " + diceOne.getFaceValue() + " " + diceTwo.getFaceValue() + " " + diceThree.getFaceValue() );
        }
        while (aNumber > 0 );


        //get input of base and height
        //aDice = iBox.getDouble("Please enter the base of a triangle: ");

        //Get Input
        //aNumber = iBox.getInteger("Enter 1 to throw the dice, or 0 to exit: ");

        System.exit(0);
        //end.

    }

类骰子如下

class Dice
{
    //public static void main(String args[])

    //data
    //private constants
    final int NUMBER_OF_SIDES = 6;

    //private variables
    private int faceValue;

    //constructors
    public Dice()
    {
        this.faceValue = 0;     //zero if not thrown.
    }

    //methods - behavious
    public void throwDice()
    {
        this.faceValue = 1 + (int) (Math.random() * NUMBER_OF_SIDES);
    }

    //method - get (accessors) and sets (mutators)
    public int getFaceValue()
    {
        return(this.faceValue);
        //System.out.println ("Dice: "+ .faceValue()); 
    }   
}

编译器结果如下 这是一个结果不确定我是否输入三个骰子对象并各扔一个?不过好像不是每个都折腾?

![在此处输入图像描述][2]

4

1 回答 1

1

没有变量“骰子”。Dice[1] 将是通过名为“Dice”的变量访问的数组中的第二项。只需让它“骰子:”+ faceValue

于 2013-05-31T11:48:03.657 回答