-4

三个骰子对象并各掷一个投掷的结果应显示在输出窗口中。我正在从我以前的工作练习中进行这个练习(PS。重用此代码来掷骰子)。我有两个课程:App 和 Dice 课程。骰子课很好。

  • 我已经设置了三个骰子对象,它的工作原理如下图所示。
  • 我正在尝试在课堂 App 中掷骰子
  • 您可以看到如下结果的屏幕截图,但不是每个都折腾?
  • 如果您(专家)向我展示如何掷骰子,我将不胜感激,好吗?

编辑:这篇文章与另一篇文章链接:m https://stackoverflow.com/questions/16878070/alternative-dice-toss-technique这个问题的不同之处在于分解我经过试验和测试的代码 [of 3 dice tossed同时从以前的帖子] 掷骰子一次三次。

类应用程序如下 - 完整的类应用程序代码 - 更新

import javabook.*;

class App
{

public static void main(String args[])
{
    App thisProgram = new App();    

}   
    //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 numThrown = 0;
        //while(numThrown < 4)      //UPDATED - commented out
        //int afaceValue;

        //declare objects
        MainWindow mWindow;
        //Dice aDice;           //UPDATED - commented out
        InputBox iBox;
        OutputBox oBox;

        //create objects                
        mWindow = new MainWindow(); 
        //aDice = new Dice();       //UPDATED - commented out       
        iBox = new InputBox(mWindow);   
        oBox = new OutputBox(mWindow);  

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

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

        while(numThrown < 3) //<4
    {
            Dice dice = new Dice();
        aNumber = iBox.getInteger("Enter 1 to throw the dice, or 0 to exit: ");
        if(aNumber == 1)
            {
                dice.throwDice();
                int rollledNum = dice.getFaceValue();
                oBox.println( "You threw : " + dice.getFaceValue() );
            } 
        else
            {
        return;
            }

        numThrown++;
            }
    }
}

结果的 屏幕抓​​取如下使用上面的代码更新了下面的屏幕抓取: 在此处输入图像描述

这就是它的样子——以前

类骰子如下这没关系。

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

2 回答 2

2
int aNumber = 0;
int numThrown = 0;
Dice dice = new Dice();
while(numThrown < 3)
{
    aNumber = iBox.getInteger("Enter 1 to throw the dice, or 0 to exit: ");
    if(aNumber == 1)
    {
        dice.throwDice();
        int rollledNum = dice.getFaceValue();
        oBox.println( "You threw : " + dice.getFaceValue() );
    } 
    else
    {
        return;
    }

    numThrown++;
}
于 2013-05-31T19:39:41.140 回答
1

这将允许你一次掷一个骰子(如果你愿意,可以掷三个以上的骰子。)

 Dice dice = new Dice(); // 1 dice for all rolls.

while(true){

        aNumber = iBox.getInteger("Enter 1 to throw the dice, or 0 to exit: ");
        if(aNumber == 0){
           break; // bust out of the loop if user enters 0.
         }
        dice.throwDice();
        oBox.println( "You threw : " + dice.getFaceValue() );


    }      
于 2013-05-31T19:55:15.820 回答