---------------------------------Dice.java-------------------------public class Dice private int value; public int getValue() return this.value; } public void setValue(int value) if (value <= 0) this.value = 0; return; } this.value = value % 7; } public void next() Random r = new Random(); int i=0; do i=r.nextInt(); }while(i==0); }}-----------------------------Main method--------------------public static void main(String[] args) Dice diceA = new Dice(); Dice diceB = new Dice(); diceA.next(); diceB.next(); if(diceA.getValue()+diceB.getValue()==7) System.out.println("WIN!!"); else System.out.println("LOSS!!"); } }
5,java中编程实现如下的骰子游戏丢下两个骰子若分值的总值为7点则
public class Test public static void main(String[] args) DieGame dieGame = new DieGame(); if (dieGame.play()) System.out.println("你赢了!"); } else System.out.println("你输了!"); } }}class Die private int faceValue; public int getFaceValue() return faceValue; } public void setFaceValue(int faceValue) this.faceValue = faceValue; } public void roll() this.faceValue = (int) (Math.random() * 6 + 1); }}class DieGame private Die die1 = new Die(); private Die die2 = new Die(); public boolean play() die1.roll(); System.out.println("第一次点数:" + die1.getFaceValue()); die2.roll(); System.out.println("第二次点数:" + die2.getFaceValue()); if (die1.getFaceValue() + die2.getFaceValue() == 7) return true; } else return false; } }}