Java:缺少变量作为骰子和值

时间:2013-05-31 10:47:35

标签: java

三个骰子对象并抛出每一个。折腾的结果应显示在输出窗口中。我正在做这个练习。我有两个课程: App Dice 要使用的课程,就是这样。

我不确定如何让它在App App中运行。如果我误解了Class App,请纠正我。

更新:

类App上没有读取任何变量的错误,我使用“Dice”作为Dice [1]错误,它应该读作 face.Value 使用下面接受的答案“** Dice:”+ faceValue。作为以下代码的解决方案: //更新的代码和更正 oBox.println(“你扔了:”+ diceOne.getFaceValue()+“”+ diceTwo.getFaceValue()+“”+ diceThree.getFaceValue()); **

类App如下

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()); 
    }   
}

编译结果如下 This is a result不确定我是否输入三个骰子对象并抛出每个骰子?但它看起来不像每个人折腾?

![在此输入图片说明] [2]

1 个答案:

答案 0 :(得分:1)

没有变量“骰子”。 Dice [1]将是通过名为“Dice”的变量访问的Array中的第2项。只需将其设为“骰子:”+ faceValue