我正在编写一个掷骰子的程序。我对 java 还是很陌生,因为我正在上课。我在这个程序的不同包中使用多个类,我想弄清楚的是,在一个类中,对于我的包 pairOfDice,我在类 pairOfDice、die1 和 die2 中创建了对象。现在我有另一个包 rollDice,我的目标是使用 pairOfDice 类来掷两个骰子并显示掷骰子。我正在努力解决的是如何做到这一点。当我掷骰子时,我的结果显示就好像我只掷了一个骰子。我已经进行了调整,以每卷显示两个骰子,尽管感觉好像我没有以更熟练的方式进行操作。
package die;
import java.util.Scanner;
/**
*
* @author <a href= "mailto:adavi125@my.chemeketa.edu" >Aaron Davis</a>
*/
public class RollDice
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PairOfDice dice = new PairOfDice();
// get amount of rolls from user
System.out.print("How many rolls do you want? ");
int numRolls = scan.nextInt();
int diceOne, diceTwo;
int boxCar, snakeEyes;
int j = 0, k = 0;
// rolls the dice the requested amount of times
for (int i = 0; i < numRolls; i++)
{
// first die to roll
diceOne = dice.roll();
// second die to roll
diceTwo = dice.roll();
// display rolled dice
System.out.println(diceOne + " " + diceTwo + "\n");
// store and display pairs of 1 rolls
if (diceOne == 1 && diceTwo == 1)
{
snakeEyes = ++j;
System.out.println("\nThe number of snake eyes you have is: "
+ snakeEyes + "\n");
}
// store and display pairs of 6 rolls
if (diceOne == 6 && diceTwo == 6)
{
boxCar = ++k;
System.out.println("\nThe number of box cars you have is: "
+ boxCar + "\n");
}
}
}
}
******************************************************************************
/*
the integers diceOne and diceTwo are my workarounds, my other package contains
public class PairOfDice extends Die
{
Die die1, die2;
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
public PairOfDice(int face)
{
die1 = new Die(face);
die2 = new Die(face);
}
}
*/
******************************************************************************
// i am un-clear how to make "PairOfDice dice = new PairOfDice();" come out as two die