创建一程序,该程序包括一个筛子类,后者有三个数据成员,即筛子的面数,筛子的点数以及包含随机数类的静态数据成员。为这个类声明一个名为roll()的成员方法,它以随机的方式返回下一次掷筛子得的点数。
import java.util.Random;
class Dice {
private int side = 6;
private int point;
private int i;
public Dice() {
roll();
System.out.print("掷筛子得的点数:" + this.point);
}
public int roll() {
this.i = new Random().nextInt(side) + 1;
this.point = this.i;
return this.i;
}
}
public class Test {
public static void main(String[] args) {
Dice d = new Dice(); //验证程序
}
}