总计一个骰子和第二个骰子面的骰子程序无法正常工作
dice program that totals the face of one die and a second die not working correctly
我正在尝试创建一个程序来掷 2 个骰子,给出 die1
的数量和 die2
的数量,然后将它们加在一起。我正在获取骰子的随机数,但是当它们被加在一起(求和)时,总数不正确。
我试过改faceValue
,numValue
,我把总和改成die1.getFaceValue + die2.getFaceValue
,但是总和出来的总和不对。如果有人可以查看代码并查看我是否将其他所有内容都放在正确的位置。提前致谢。
package assignment3;
import java.util.*;
public class Die {
private static final int DEFAULT_NUM_FACES =6;
private static final int value = 1;
private static int faceValue;
private static int numFaces;
//die(int, int) method- constructor that initialized the instance variables to the parameter values
public Die(int die1, int die2) {
}
//Die(int) method- constructor that initialized the instance variables faceValue to the parameter value and numFaces to
//DEFAULT_NUM_FACES
public Die(int value) {
faceValue = value;
numFaces = DEFAULT_NUM_FACES;
}
//die() default constructor that initialized both instance variables to DEFAULT_NUM_FACES
public Die() {
this.faceValue=DEFAULT_NUM_FACES;
this.numFaces=DEFAULT_NUM_FACES;
}
//die(die) copy constructor
public Die(Die otherDie)
{
new Die();
}
// getFaceValue() - returns the faceValue of the instance
public int getFaceValue() {
return faceValue;
}
// getNumFaces - return the numFaces of the Die
public int getNumFaces()
{
return numFaces;
}
//setFaceValule - sets face values of the die to supplied value
public int setValue(int faceValue) {
return faceValue;
}
//toString returns a string representation of the face value in the form of (faceValue)
public String toString()
{
String result = Integer.toBinaryString(faceValue);
return result;
}
//Roll - rolls the die to generate a new face value. The instances face value will be set to this new value.
//You can use the random method of the Math class to accomplish this task. The random method generates a random number between 0 and 1.
//by multiplying this number by number of faces, casting the result to an integer and adding one to it.
//you will be able to generate a random number between 1 and numFaces
public int roll()
{
faceValue = (int )(Math.random()*numFaces+1);
return faceValue;
}
public static void main(String[] args) {
Die die1;
Die die2;
int sum;
die1= new Die();
die1.roll();
die2= new Die();
die2.roll();
sum = (die1.getFaceValue())+(die2.getFaceValue()) ;
System.out.println("Toss 0 generated a " + die1.getFaceValue()+ " and a " + die2.getFaceValue() +" for a total of " +sum);
}
}
这应该掷骰子 1 并给我骰子 1 的面值,然后它应该给我骰子 2 的值,然后它应该是两个骰子的总和。
每个 Die
您应该只调用 roll
一次。
每次你调用 roll
它都会创建一个新的随机数并将其分配给 faceValue
die1= new Die();
die1.roll();
die2= new Die();
die2.roll();
然后使用面值
sum = (die1.getFaceValue())+(die2.getFaceValue()) ;
我认为你应该有一个实例变量而不是 class 变量。
基本上,只有该变量的一个副本与 class 的所有对象(实例)共享。如果更改该变量,所有其他 class 对象将看到该更改。
只需从局部变量中删除 'static' 关键字。
public class Die {
private static final int DEFAULT_NUM_FACES = 6;
private int faceValue;
private int numFaces;
public Die() {
this.faceValue = DEFAULT_NUM_FACES;
this.numFaces = DEFAULT_NUM_FACES;
}
// getFaceValue() - returns the faceValue of the instance
public int getFaceValue() {
return faceValue;
}
// getNumFaces - return the numFaces of the Die
public int getNumFaces() {
return numFaces;
}
// setFaceValule - sets face values of the die to supplied value
public int setValue(int faceValue) {
return faceValue;
}
// toString returns a string representation of the face value in the form of
// (faceValue)
public String toString() {
String result = Integer.toBinaryString(faceValue);
return result;
}
public int roll() {
faceValue = (int) (Math.random() * numFaces + 1);
return faceValue;
}
public static void main(String[] args) {
Die die1;
Die die2;
int sum;
die1 = new Die();
die1.roll();
die2 = new Die();
die2.roll();
sum = (die1.getFaceValue()) + (die2.getFaceValue());
System.out.println("Toss 0 generated a " + die1.getFaceValue() + " and a " + die2.getFaceValue()
+ " for a total of " + sum);
}
}
祝你有美好的一天..
我正在尝试创建一个程序来掷 2 个骰子,给出 die1
的数量和 die2
的数量,然后将它们加在一起。我正在获取骰子的随机数,但是当它们被加在一起(求和)时,总数不正确。
我试过改faceValue
,numValue
,我把总和改成die1.getFaceValue + die2.getFaceValue
,但是总和出来的总和不对。如果有人可以查看代码并查看我是否将其他所有内容都放在正确的位置。提前致谢。
package assignment3;
import java.util.*;
public class Die {
private static final int DEFAULT_NUM_FACES =6;
private static final int value = 1;
private static int faceValue;
private static int numFaces;
//die(int, int) method- constructor that initialized the instance variables to the parameter values
public Die(int die1, int die2) {
}
//Die(int) method- constructor that initialized the instance variables faceValue to the parameter value and numFaces to
//DEFAULT_NUM_FACES
public Die(int value) {
faceValue = value;
numFaces = DEFAULT_NUM_FACES;
}
//die() default constructor that initialized both instance variables to DEFAULT_NUM_FACES
public Die() {
this.faceValue=DEFAULT_NUM_FACES;
this.numFaces=DEFAULT_NUM_FACES;
}
//die(die) copy constructor
public Die(Die otherDie)
{
new Die();
}
// getFaceValue() - returns the faceValue of the instance
public int getFaceValue() {
return faceValue;
}
// getNumFaces - return the numFaces of the Die
public int getNumFaces()
{
return numFaces;
}
//setFaceValule - sets face values of the die to supplied value
public int setValue(int faceValue) {
return faceValue;
}
//toString returns a string representation of the face value in the form of (faceValue)
public String toString()
{
String result = Integer.toBinaryString(faceValue);
return result;
}
//Roll - rolls the die to generate a new face value. The instances face value will be set to this new value.
//You can use the random method of the Math class to accomplish this task. The random method generates a random number between 0 and 1.
//by multiplying this number by number of faces, casting the result to an integer and adding one to it.
//you will be able to generate a random number between 1 and numFaces
public int roll()
{
faceValue = (int )(Math.random()*numFaces+1);
return faceValue;
}
public static void main(String[] args) {
Die die1;
Die die2;
int sum;
die1= new Die();
die1.roll();
die2= new Die();
die2.roll();
sum = (die1.getFaceValue())+(die2.getFaceValue()) ;
System.out.println("Toss 0 generated a " + die1.getFaceValue()+ " and a " + die2.getFaceValue() +" for a total of " +sum);
}
}
这应该掷骰子 1 并给我骰子 1 的面值,然后它应该给我骰子 2 的值,然后它应该是两个骰子的总和。
每个 Die
您应该只调用 roll
一次。
每次你调用 roll
它都会创建一个新的随机数并将其分配给 faceValue
die1= new Die();
die1.roll();
die2= new Die();
die2.roll();
然后使用面值
sum = (die1.getFaceValue())+(die2.getFaceValue()) ;
我认为你应该有一个实例变量而不是 class 变量。
基本上,只有该变量的一个副本与 class 的所有对象(实例)共享。如果更改该变量,所有其他 class 对象将看到该更改。
只需从局部变量中删除 'static' 关键字。
public class Die {
private static final int DEFAULT_NUM_FACES = 6;
private int faceValue;
private int numFaces;
public Die() {
this.faceValue = DEFAULT_NUM_FACES;
this.numFaces = DEFAULT_NUM_FACES;
}
// getFaceValue() - returns the faceValue of the instance
public int getFaceValue() {
return faceValue;
}
// getNumFaces - return the numFaces of the Die
public int getNumFaces() {
return numFaces;
}
// setFaceValule - sets face values of the die to supplied value
public int setValue(int faceValue) {
return faceValue;
}
// toString returns a string representation of the face value in the form of
// (faceValue)
public String toString() {
String result = Integer.toBinaryString(faceValue);
return result;
}
public int roll() {
faceValue = (int) (Math.random() * numFaces + 1);
return faceValue;
}
public static void main(String[] args) {
Die die1;
Die die2;
int sum;
die1 = new Die();
die1.roll();
die2 = new Die();
die2.roll();
sum = (die1.getFaceValue()) + (die2.getFaceValue());
System.out.println("Toss 0 generated a " + die1.getFaceValue() + " and a " + die2.getFaceValue()
+ " for a total of " + sum);
}
}
祝你有美好的一天..