天天看點

簡易對話機器人

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 "請輸入正确中文或數字字元";

}

}

gui

繼續閱讀