📄 textdisplay.java
字号:
import org.jcsp.lang.CSProcess;
import org.jcsp.lang.ChannelInput;
class TextDisplay implements CSProcess {
// attributes
private final ChannelInput in;
private final int nPhilosophers;
private final static String[] space =
{" ", " ", " ", " ", " "};
// constructors
public TextDisplay (int nPhilosophers, ChannelInput in) {
this.nPhilosophers = nPhilosophers;
this.in = in;
}
// public methods
public void run () {
ISReport report;
int id, state, philId;
System.out.println ("\nThe Game is runing: " + nPhilosophers +
" 个哲学家就餐问题\n");
while (true) {
report = (ISReport) in.read ();
id = report.getId ();
state = report.getState ();
if (report instanceof PhilReport) {
switch (state) {
case PhilReport.THINKING:
System.out.println (space[id] + "哲学家 " + id +
" 在思考 ...");
break;
case PhilReport.HUNGRY:
System.out.println (space[id] + "哲学家 " + id +
" 饿了 ...");
break;
case PhilReport.SITTING:
System.out.println (space[id] + "哲学家 " + id +
" 就坐 ...");
break;
case PhilReport.EATING:
System.out.println (space[id] + "哲学家 " + id +
" 用餐 ...");
break;
case PhilReport.LEAVING:
System.out.println (space[id] + "哲学家 " + id +
" 离开 ...");
break;
}
} else if (report instanceof ForkReport) {
philId = ((ForkReport) report).getPhilId ();
switch (state) {
case ForkReport.DOWN:
System.out.println (space[philId] + "哲学家 " + philId +
" 放下筷子 " + id + " ...");
break;
case ForkReport.UP:
System.out.println (space[philId] + "哲学家 " + philId +
" 拿起筷子 " + id + " ...");
break;
}
} else if (report instanceof SecurityReport) {
System.out.println ("安全: " + id + " 坐下 ...");
} else if (report instanceof ClockReport) {
System.out.println ("\n第 " + id + "轮\n");
} else {
System.out.println ("\nBad report !!!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -