3 つのサイコロ オブジェクトをそれぞれトスします。トスの結果は出力ウィンドウに表示されます。この練習に取り組んでいます。私は 2 つのクラスを持っています: AppとDiceのクラスを操作するだけです。
クラスAppで機能させる方法がわかりません。以下のように Class App を間違えた場合は訂正してください。
更新しました:
変数を読み取らないクラス App にエラーがあり、「Dice」を Dice [ 1 ] として誤って使用したため、代わりにface.Valueとして読み取る必要があります。以下のように受け入れられた回答を使用する" **Dice: "+ faceValue. 次のコードの解決策として: //UPDATED CODE and CORRECTED oBox.println( "You throww : " + 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());
}
}
コンパイラの結果は以下のとおり しかし、それはそれぞれを投げるように見えませんか?
![ここに画像の説明を入力][2]