简易对话机器人

import java.io.*; 
import javax.swing.*; 
import java.awt.*; 
import java.util.*; 
import java.awt.event.*; 
//实现功能  过滤文本,一问多答,字符串检测,空格删除 
class robots extends JFrame 
{
JLabel j1;
JLabel j2;
JPanel p1,p2;
JButton b1;
JTextField f1;
JTextArea a1,a2;
FileReader fr1;
String question[]=new String[100];//用于存储问题
String answer[]=new String[100];//用于存储答案
Random rd=new Random();//生成随机数
ArrayList<String> list=new ArrayList();//存储相同问题下的不同回答
int count,a,b=0;
int ans=0;
robots(){
setTitle("机器人对话");
setSize(600,300);
setLayout(new GridLayout(1,2));
p1=new Mypanel1();
p2=new Mypanel2();
add(p1);
add(p2);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
String s="";
try{
fr1=new FileReader(new File("E:\\Editplus\\GUI\\机器人\\对话内容.txt"));
BufferedReader br1=new BufferedReader(fr1);
while((s=br1.readLine())!=null){
if(count%2==0)
question[a++]=s;
count++;
}
br1.close();
}
catch(Exception e){
System.out.println("没有找到指定文件");
}
try{
fr1=new FileReader(new File("E:\\Editplus\\GUI\\机器人\\对话内容.txt"));
BufferedReader br2=new BufferedReader(fr1);
count=0;
while((s=br2.readLine())!=null){
if(count%2==1)
answer[b++]=s;
count++;
}
br2.close();
fr1.close();
}
catch(Exception e){
System.out.println("没有找到指定文件");
}
b1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
String str=f1.getText().trim();//清楚空格
char []ch=str.toCharArray();
try{
if(check(ch)){
throw new StringException();
}
}
catch(StringException x){
a1.append("请输入正确中文或数字字符!!!\n");
}
//比较字字符串与数组里的字符串
finally{
int z=0;
list.clear();//每次匹配时清空回答列表
str=filter(ch);
String mohu[]=mohuchaxun(str);
for(int i=0;i<count/2;i++){ 
for(int j=0;j<mohu.length;j++){
if(question[i].contains(mohu[j]))//符合条件的相同问题不同答案
{
list.add(answer[i]);//将回答添加到list列表里
z++;
}
}
}
if(z!=0){//回答列表不为0
int num=0;
if(list.size()>1)num=rd.nextInt(list.size()-1);
String answer=list.get(num);
a1.append(f1.getText()+"\n");
a1.append(answer+"\n");
}
else{//回答列表为0
String hd="";
int more = rd.nextInt(5); 
switch(more)
{
case 0:hd="这个问题太难了,换一个吧";break;
case 1:hd="我偏不告诉你";break;
case 2:hd="总么问这个问题";break;
case 3:hd="你猜这个问题我会不会";break;
case 4:hd="我才不告诉你";break;
}
a1.append(hd+"\n");
}
}
}
}
);
}
public boolean check(char ch[]){//存在非汉字或数字则返回false
boolean X=false;
for(int i=0;i<ch.length;i++){
if(!((ch[i]>=48&&ch[i]<=57)||(vd(ch[i])))){
X=true;
}
}
return X;
}
public boolean vd(char a){//判断是否是汉字  
byte[] bytes=(""+a).getBytes();   
   if(bytes.length==2){   
        int[] ints=new int[2];   
        ints[0]=bytes[0]& 0xff;   
        ints[1]=bytes[1]& 0xff;   
        if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){   
             return true;
            }   
         }
      return false;
}
public String filter(char[] ch){//将一段字符串中的汉字过滤出来
ArrayList list=new ArrayList();
for(int i=0;i<ch.length;i++){
if(vd(ch[i])){
list.add(ch[i]);
}
}
StringBuilder sb=new StringBuilder();
for(int i=0;i<list.size();i++)sb.append(list.get(i));
String s=sb.toString();
return s;
}
public static String[] mohuchaxun(String s){//将问题切分为2个汉字一组的关键字数组
char ch[]=s.toCharArray();
if(ch.length%2==0){
String snum[]=new String[ch.length/2];
int t=0;
for(int i=0;i<ch.length;i+=2){
snum[t++]=""+ch[i]+ch[i+1];
}
return snum;
}
else{
String snum[]=new String[ch.length/2+1];
int t=0;
for(int i=0;i<ch.length-1;i+=2){
snum[t++]=""+ch[i]+ch[i+1];
}
snum[ch.length/2]=""+ch[ch.length-1];
return snum;
}
}
class Mypanel1 extends JPanel
{
Mypanel1(){
j1=new JLabel("输入问题");
f1=new JTextField(25);
b1=new JButton("发送");
this.add(j1);
this.add(f1);
this.add(b1);
}
}
class Mypanel2 extends JPanel
{
Mypanel2(){
setLayout(new BorderLayout(5,0));
j2=new JLabel("机器人的回答",JLabel.CENTER);
a1=new JTextArea(0,10);
JLabel j3=new JLabel(" ");
JLabel j4=new JLabel(" ");
this.add(j2,BorderLayout.NORTH);
this.add(new JScrollPane(a1),BorderLayout.CENTER);
this.add(j3,BorderLayout.SOUTH);
this.add(j4,BorderLayout.EAST);
}
}
public static void main(String[] args) 
{
new robots(); 
}
}
class StringException extends Exception
{
public String toString(){
return "请输入正确中文或数字字符";
}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值