public static void main(String[] args) throws IOException {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("你好","您好,主人");
hm.put("我饿了", "您想吃什么,但是我不会做");
hm.put("拜拜", "拜拜");
Robot r = new Robot(hm);
// 先判断机器人状态,如果是closed的,则将状态改为open
if(r.getState().equals("closed")){
r.setState("open");
}
// 先创建键盘录入对象
Scanner sc = new Scanner(System.in);
// 还要创建具有缓冲区的字符输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("info.txt"));
String str = "对不起,我不知道怎么回答,请换个问题问我";
System.out.println("Robot状态已是\"open\",可以开始对话:");
while(true){
String line = sc.nextLine();
if(r.getResult().containsKey(line)){
if(line.equals("拜拜")){
System.out.println(r.getResult().get(line));
bw.write(line + "-" +r.getResult().get(line));
bw.newLine();
bw.close();
break;
}else{
System.out.println(r.getResult().get(line));
bw.write(line + "-" +r.getResult().get(line));
bw.newLine();
}
// 如果用户输入的指令没有对应的应答,则提示“对不起,我不知道怎么回答,请换个问题问我”
}else{
System.out.println(str);
bw.write(line + "-" +str);
bw.newLine();
}
}
}
}
class Robot {
public static String state = "closed";
private HashMap<String,String> result;
public Robot() {
super();
}
public Robot(HashMap<String, String> result) {
super();
this.result = result;
}
public static String getState() {
return state;
}
public static void setState(String state) {
Robot.state = state;
}
public HashMap<String, String> getResult() {
return result;
}
public void setResult(HashMap<String, String> result) {
this.result = result;
}
}
|