lgg3vs985:java高手些,进来帮帮忙

来源:百度文库 编辑:高考问答 时间:2024/04/29 06:07:47
请各位帮俺编一道程序,万分感谢!
Question 6: Write a class named PairOfDice. An object of this class will represent a pair of dice. It will contain two instance variables to represent the numbers showing on the dice and an instance method for rolling the dice. Your instance variables must be private, and you must test your class with a short program that counts how many times a pair of dice is rolled, before the total of the two dice is equal to two.

public class PairOfDice {
private int die1;
private int die2;
public static void main(String[] args) {
int numberOfRolls;
PairOfDice pairDice = new PairOfDice();
numberOfRolls = pairDice.rollFor(2);
System.out.println("It took " + numberOfRolls + " rolls before the total of the two dice is equal to two.");
}

public int rollFor(int n) {
int roll;
int rollCt;
rollCt = 0;
do {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;
rollCt++;
} while (roll != n);
return rollCt;
}
}

我不会
不过你给的分太低了
人家不一定有积极性的