-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatTool.java
70 lines (66 loc) · 1.95 KB
/
ChatTool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
实现伪AI聊天机器人
需求:
A:读取用户输入字符串
B:随机回复"是吗?"、"然后呢?"、"这样啊"和重复用户输入的最后一句话。
*/
import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Random;
class ChatTool
{
private static String reply="";
private static int max=4;
private static int min=1;
private static boolean flag=true;
private ChatTool(){
}
public static void chat(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println("时间:"+df.format(new Date()));
System.out.println("最近还好吗?");
Scanner sc=new Scanner(System.in);
while(flag)
{
String chatString=sc.nextLine();
if(chatString.isEmpty())
{
flag=false;
System.out.println("时间:"+df.format(new Date()));
System.out.println("你不想说就下次再聊吧。");
}else if(chatString.contains("拜拜")||chatString.contains("再见")){
flag=false;
System.out.println("时间:"+df.format(new Date()));
System.out.println("好的,下次再聊吧。");
}else
{
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
switch(s){
case 1:
System.out.println("时间:"+df.format(new Date()));
reply ="然后呢?";
break;
case 2:
System.out.println("时间:"+df.format(new Date()));
if(chatString.contains(",")){
reply=chatString.substring(chatString.lastIndexOf(",")+1)+"?";
}
else{
reply=chatString+"?";
}
break;
case 3:
System.out.println("时间:"+df.format(new Date()));
reply ="是吗?";
break;
default :
System.out.println("时间:"+df.format(new Date()));
reply="这样啊";
}
System.out.println(reply);
}
}
}
}