java实现 应激方式式问答助手

从http://bbs.csdn.net/wap/topics/390833266借鉴的,感觉不错,可以学习。


import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.*;

public class MainClass extends JFrame implements ActionListener{

    JButton an1,an2;

    JTextField bjk;

    JTextArea area;

    JLabel bq1,bq2;

    JPanel p1;      //声明所有按钮

    public static void main(String[] args){

        Write.read();   //应该是读取用户所写数据

        //System.out.println("chushihua");

        new MainClass();

    }

    public MainClass(){



        an1=new JButton("发送");

        an2=new JButton("退出");

        bjk=new JTextField(10);//输入框长度

        area=new JTextArea();

        bq1=new JLabel("这里是[居正]出品的聊天机器人,快跟它打个招呼吧~");

        bq2=new JLabel("请输入内容并发送:");



        p1=new JPanel();

        p1.add(bq2);p1.add(bjk);p1.add(an1);p1.add(an2);   //下面四个按钮添加到面板



        an1.addActionListener(this);

        an1.setActionCommand("go");  //设置变量,让actionperform来选择对应处理函数

        

        an2.addActionListener(this);

        an2.setActionCommand("exit");

        this.add(bq1,BorderLayout.NORTH);//this指代MainClass初始化

        this.add(area);                  //中间输入版块

        this.add(p1,BorderLayout.SOUTH);  //底下的四个按钮

        this.setTitle("[居正]聊天机器人");   //软件的名称

        this.setSize(500,400);     

        this.setLocation(100,100);  

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setVisible(true);

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        if(e.getActionCommand().equals("go")){

            String text=bjk.getText();   //读取输入框

            //System.out.println(text);

            String texttest=robotedit();  //载入下一个函数

            System.out.println("texttest:"+texttest);

            area.setText("    我说:\n"+text+"\n\n    [JUZEON]Robot说:\n"+texttest);  //在整个文本上的记录

            bjk.setText("");   //重置整个数据库

        }else if(e.getActionCommand().equals("exit")){

            System.exit(0);

        }

    }

    public String robotedit(){

        /String re="我还在进步,有些问题回答不上来哦~Sorry~";

        try{

        for(int i=0;i<Write.wen.length;i++){

            String mywen=Write.wen[i];

            String tada=Write.da[i];          //循环遍历拆分后的语句

            System.out.println(mywen);

            System.out.println(tada);

            String text=bjk.getText();  //读取用户输入的句子

            int bl=text.indexOf(mywen);    



            if((bl)==(-1)){    //没找到会自动输出



            }else{

                return tada;   //如果b1存在,则输出这个答案

            }





        }

        }catch(Exception ergg){

            //return "我还在进步,有些问题回答不上来哦~Sorry~";

        }

        System.out.println("到了返回这里了~");

        return "我还在进步,有些问题回答不上来哦~Sorry~";

        

    }



}

class Write{

    static String wen[]=new String[1000];

    static String da[]=new String[1000];  //创建两个问答字符串



    static FileWriter fw;

    static BufferedWriter bw;//定义文件写、字节流写声明



    static FileReader fr;

    static BufferedReader br;//定义文件读、字节流读声明

    static int shu=0;

    public static void write(){

        try{              //写数据到这个txt文件夹

            fw=new FileWriter("./talk.txt");

            bw=new BufferedWriter(fw);

            bw.write("你好 见到你真高兴~\n");

            bw.write("作者 是居正这个BT的家伙创造了我~\n");

            bw.write("住 我住在我家~\n");

            bw.write("石头 我出布,你输了~\n");

            bw.write("剪刀 我出石头,你输了~\n");

            bw.write("布 我出剪刀,我赢了~~\n");

            bw.write("玩 来玩石头剪刀布吧~\n");

        }catch(Exception e){}

        finally{

            try{

                bw.flush();  //强制将缓冲区释放

                fw.close();

                bw.close();

            }catch(Exception e){System.out.println(e.toString());}

        }

    }

    public static void read(){

        System.out.println("readStart");

        try{

            File f=new File("./talk.txt");  //建立该txt

            System.out.println("set");

            if(f.exists()==false){             //如果文件不存在,则返回错误

                write();

                System.out.println("write");

            }

            fr=new FileReader("./talk.txt");  //读取这个文件

            br=new BufferedReader(fr);



            String s="";

            int i=0;

            while((s=br.readLine())!=null){

                System.out.println(s);

                String[] st=s.split(" ");

                System.err.println(st[0]);

                System.err.println(st[1]);

                wen[i]=st[0];

                da[i]=st[1];

                i++;

                System.out.println("No."+i+":"+wen[i]);  //把write中句子都放在句子wen[i]和da[i]字符串中

                System.out.println("No."+i+":"+da[i]);

            }



        }catch(Exception e){System.err.println(e.toString());}



    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值