当前位置 : 主页 > 编程语言 > c++ >

The Die of Zillionaire

来源:互联网 收集:自由互联 发布时间:2021-06-30
Die.java import java.util.HashMap;import java.util.Map;import java.util.Random;//定义一个骰子类且面值只能为正整数public class Die{ private int id = 1;//骰子投掷的次数编号 private int maxValue = 6; private int minVal
Die.java
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

//定义一个骰子类且面值只能为正整数
public class Die{
   private int id = 1;//骰子投掷的次数编号
   private int maxValue = 6;
   private int minValue = 1;
   private int faceValue = 0;//默认朝上面值为0,表示未投掷过骰子
   private Map
 
   dieMap = new HashMap
  
   (); public void die(int id){ this.id = id; } public void detail(){//显示骰子详细信息,仅能显示骰子当前设定的面值范围,未完成:每次投掷时的骰子面值范围 int i = 1; if (faceValue == 0) { System.out.println("骰子的信息如下:"); System.out.println("此骰子还未投掷过"); System.out.println("当前最大面值:" + getMaxValue()); System.out.println("当前最小面值:" + getMinValue()); } else{ System.out.println("骰子的信息如下:"); System.out.println("此骰子已投掷:" + getId() + "次"); System.out.println("当前最大面值:" + getMaxValue()); System.out.println("当前最小面值:" + getMinValue()); for (i = 1;i<=getId();i++){ System.out.println("第" + i + "次投掷结果为:" + getValue(i)); } } System.out.println("\n"); } public void initDie(){//初始化骰子 this.id = 1; this.maxValue = 6; this.minValue = 1; this.faceValue = 0; dieMap.clear(); } public int getId(){//获取此骰子已投掷的次数 if (id == 1) return id; else return id-1; } public void setMaxValue(int maxValue){//设置投掷最大面值 this.maxValue = maxValue; } public int getMaxValue(){//获得骰子当前最大面值 return maxValue; } public void setMinValue(int minValue){//设置投掷最小面值 this.minValue = minValue; } public int getMinValue(){//获得骰子当前最小面值 return minValue; } public int getValue(){//获取当前次骰子投掷的朝上面值 if (faceValue == 0) return faceValue;//未投掷骰子时的返回值 else{ this.faceValue = dieMap.get(id-1); return faceValue; } } public int getValue(int id){//获得第id次投掷时骰子朝上面值 if (faceValue == 0 || id > this.id-1) return 0;//未投掷骰子时的返回值 else{ this.faceValue = dieMap.get(id); return faceValue; } } public void roll(){//投掷一次骰子 Random result = new Random(); faceValue = result.nextInt(maxValue - minValue +1) + minValue; dieMap.put(id,faceValue); System.out.println("此次为您第" + id + "次投掷此骰子"); id++; } public void roll(int number){//连续投掷number次骰子 Random result = new Random(); int i = id; int k = id + number; for (i = id; i < k; i++){ faceValue = result.nextInt(maxValue - minValue +1) + minValue; dieMap.put(id,faceValue); System.out.println("此次为您第" + id + "次投掷此骰子"); id++; } } public void rollNoNumber(int noNumber){//投掷骰子且不希望某个数(noNumber)出现 Random result = new Random(); faceValue = result.nextInt(maxValue - minValue +1) + minValue; while (faceValue == noNumber){ faceValue = result.nextInt(maxValue - minValue +1) + minValue; } dieMap.put(id,faceValue); System.out.println("此次为您第" + id + "次投掷此骰子" + "且本次不出现正整数" + noNumber); id++; } }
  
 
网友评论