📄 model.java
字号:
/**
*
*/
package hnu.software.pfdai;
import java.util.Observable;
import java.util.Random;
/**
* @author daipengfei
*
*/
public class Model extends Observable {
private int times;// 总次数
private boolean[] door;
private int choice;
private int tipChoice;
private double result;
private String resultDetail;
private String detail;
private double rightCount;// 正确次数
private double count;// 选择次数
/**
* @return the door
*/
public boolean[] getDoor() {
return door;
}
/**
* @param door
* the door to set
*/
public void setDoor(boolean[] door) {
this.door = door;
}
/**
* @return the tipChoice
*/
public int getTipChoice() {
return tipChoice;
}
/**
* @param tipChoice
* the tipChoice to set
*/
public void setTipChoice(int tipChoice) {
this.tipChoice = tipChoice;
}
/**
* Constructs a new Model that is initially invisible.
*
*/
public Model() {
door = new boolean[] { false, false, false };
initial();
}
/**
* initial the data
*
*/
public void initial() {
times = 0;
choice = 0;
tipChoice = 0;
result = 0;
rightCount = 0;
count = 0;
resultDetail = "";
detail = "";
}
/**
* @return the choice
*/
public int getChoice() {
return choice;
}
/**
* @param choice
* the choice to set
*/
public void setChoice(int choice) {
switch (choice) {
case 0:
this.choice = choice;
break;
case 1:
this.choice = Math.abs(new Random().nextInt() % 3);
break;
case 2:
for (int i = 0; i < door.length; i++)
if (i != tipChoice && i != choice)
this.choice = i;
}
// get the door number of the car
int rInt = 0;
for (; rInt < door.length; rInt++)
if (door[rInt] == true)
break;
// assign the result data
if (this.choice == rInt) {
rightCount++;
resultDetail += "*";
} else {
resultDetail += "!";
}
detail += rInt;
count++;
result = (rightCount / count) * 100;
// initial the door
for (int i = 0; i < door.length; i++)
door[i] = false;
this.setTimes(times - 1);
}
/**
* @return the times
*/
public int getTimes() {
return times;
}
/**
* @param times
* the times to set
*/
public void setTimes(int times) {
if (times == 0) {
for (int i = 0; i < door.length; i++)
door[i] = false;
initial();
} else {
// produce the random number
Random r = new Random();
int rInt = Math.abs(r.nextInt() % 3);
door[rInt] = true;
// choose one from doors
this.choice = (rInt * 2) % 3;
// give a tip choice
for (int i = 0; i < door.length; i++) {
if (i != this.choice && door[i] == false) {
this.tipChoice = i;
break;
}
}
}
this.times = times;
this.setChanged();// Indicates that the model has changeds
this.notifyObservers();
}
/**
* @return the detail
*/
public String getDetail() {
return detail;
}
/**
* @param detail
* the detail to set
*/
public void setDetail(String detail) {
this.detail = detail;
}
/**
* @return the result
*/
public double getResult() {
return result;
}
/**
* @param result
* the result to set
*/
public void setResult(double result) {
this.result = result;
}
/**
* @return the resultDetail
*/
public String getResultDetail() {
return resultDetail;
}
/**
* @param resultDetail
* the resultDetail to set
*/
public void setResultDetail(String resultDetail) {
this.resultDetail = resultDetail;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -