0

だから私はこれまでのところ動作しているように見えるサイコロを振るプログラムを持っていますが、私が理解できない唯一の問題は、サイコロの出力を合計する方法です。前のサイコロを合計しながら、もう一度サイコロを振ります。

これが私が始めたDieクラスです

public class Die{   //only thing changed was removing implementation of the interface

private int noFaces;

public Die() {
    noFaces = 6;
}

public int getNoFaces() {       //getter
    return noFaces;
}

public Die(int noFaces) {       //setter, sets new value according to passed parameter
    this.noFaces = noFaces;
}

public int roll() {             //generates random number+1 within specified "noFaces" range
    return (int) (Math.random() * noFaces + 1);
}

public void printRoll() {       //goes to above method roll() and prints the returned value
    System.out.println(roll());
}

}

そして、1つのDieをPairOfDiceにします。これは、合計の問題が発生している場所です

public class PairOfDice {

private Die d1, d2;

public PairOfDice() {       //initializes 2 die objects
    d1 = new Die();
    d2 = new Die();
}

public PairOfDice(int noFaces) {    //sets the dice equally
    d1 = new Die(noFaces);
    d2 = new Die(noFaces);
}   

public PairOfDice(int noFaces1, int noFaces2) { //sets dice separately
    d1 = new Die(noFaces1);
    d2 = new Die(noFaces2);
}

public void printRoll() {   //prints rolls
    System.out.println("Die 1 returned: " + d1.roll());
    System.out.println("Die 2 returned: " + d2.roll());

}

public void printRollSum(){     //print sum of both rolls
    System.out.println("Sum of both dice rolls are: " + (d1.roll() + d2.roll()));
}

}

そして、私が出力を取得している場所、RollingDice

public class RollingDice {

public static void main(String[] args) {

    PairOfDice pairOne = new PairOfDice();
    pairOne.printRollSum();
    System.out.println();

    PairOfDice pairTwo = new PairOfDice(10);
    pairTwo.printRoll();
    pairTwo.printRollSum();
    System.out.println();

    PairOfDice pairThree = new PairOfDice(100, 3);
    pairThree.printRoll();
    pairThree.printRollSum();

}

}

私のプログラムまたはコメントに関するすべてのヘルプは大歓迎です。私はまだ学んでいます。ありがとう!

4

5 に答える 5

3

2 つのサイコロの合計を取得していますが、毎回サイコロを転がしています。printRoll()ロールして値を表示してから、printRollSum()それらを再度ロールすると、異なる値が得られます。

于 2013-07-30T16:06:03.490 に答える
0

このメソッドroll()は、呼び出されるたびに新しい乱数を生成するため、呼び出すたびに新しい乱数printRoll()printRollSum()受け取ります。

クラスの 1 つにの結果を保存し、rollそれらの結果にアクセスするメソッドを提供する必要があります。

于 2013-07-30T16:07:41.903 に答える
0

解決策として、最後にロールされた値をDieクラスに保存し ( に似ていますが、たとえばnoFaces呼び出します)、printRollSum() を変更して代わりに使用することができます。lastValuegetLastValue()roll()

于 2013-07-30T16:07:52.490 に答える
0

毎回新しい番号を生成printRollprintRollSumて呼び出されます。2 つの方法を組み合わせるか、少なくともロールの現在の値をサイコロに格納し、次の方法でアクセスする必要があります。printRollSum

于 2013-07-30T16:09:42.707 に答える
0

printRoll() および printRollSum() 呼び出しのそれぞれがサイコロを再ロールしています。サイコロを振って、個々のサイコロの額面を出力し、それら 2 つの額面の値の合計を出力する場合は、ロールの結果を保存してから、それらの結果に対して合計計算を実行する必要があります。例えば:

public class PairOfDice {

private Die d1, d2;
int lastRollD1;
int lastRollD2;

public PairOfDice() {       //initializes 2 die objects
    d1 = new Die();
    d2 = new Die();
}

public PairOfDice(int noFaces) {    //sets the dice equally
    d1 = new Die(noFaces);
    d2 = new Die(noFaces);
}   

public PairOfDice(int noFaces1, int noFaces2) { //sets dice separately
    d1 = new Die(noFaces1);
    d2 = new Die(noFaces2);
}

public void roll()
{
    lastRollD1 = d1.roll();
    lastRollD2 = d2.roll();
}

public void printRoll() {   //prints rolls
    System.out.println("Die 1 returned: " + lastRollD1);
    System.out.println("Die 2 returned: " + lastRollD1);

}

public void printRollSum(){     //print sum of both rolls
    System.out.println("Sum of both dice rolls are: " + (lastRollD1 + lastRollD1));
}

}

このバージョンでは、RollingDice は次を呼び出します。

pairOne.roll();
pairOne.printRoll();
pairOne.printRollSum();
于 2013-07-30T16:12:50.823 に答える