写的不好,见谅啊- import java.util.Random;
- public class ThrowDice {
- public static void main(String[] args) {
- Dice dice1 = new Dice(1, 2, 3, 4, 5, 6);
- Dice dice2 = new Dice(1, 2, 3, 4, 5, 6);
- while(true) {
- if(getSum(dice1, dice2) == 7) {
- System.out.println("你赢了");
- break;
- } else {
- System.out.println("你输了");
- }
- }
- }
- public static int getSum(Dice dice1, Dice dice2){
- int i = dice1.throwDice();
- int j = dice1.throwDice();
- return i + j;
- }
-
- }
- class Dice { //定义一个类,代表骰子
- public static final Random r = new Random(); //定义一个随机数产生器,将其声明为常量
- private int suface1; //定义6个成员变量,代表骰子上的每一个面的点数
- private int suface2;
- private int suface3;
- private int suface4;
- private int suface5;
- private int suface6;
- public Dice(int suface1, int suface2, int suface3, int suface4,
- int suface5, int suface6) { //这个构造方法代表你要创造的骰子每个面对应的点数,貌似有点多余
- super();
- this.suface1 = suface1;
- this.suface2 = suface2;
- this.suface3 = suface3;
- this.suface4 = suface4;
- this.suface5 = suface5;
- this.suface6 = suface6;
- }
- public int throwDice() { //定义一个方法,扔骰子
- return r.nextInt(6) + 1;
- }
- }
复制代码 |