2016-02-05 251 views
-1

我想在Java中創建一個聊天機器人,但我不知道如何開始? 我試過下面的代碼,但沒有得到我確切需要的東西。 在這個程序中,我不得不寫出所有問題的可能性。 如果有任何人有解決方案,請幫助我。如何在java中製作虛擬聊天機器人?

import java.io.*; 

public class Chatbot 
{ 
public static void main(String[]args) throws IOException 
{ 
    String hel = new String("Hello"); 
    String hel1 = new String("HELLO"); 
    String hel2 = new String("hello"); 
    String hel3 = new String("Hii"); 
    String hel4 = new String("Hiii"); 
    String hel5 = new String("hii"); 
    String hel6 = new String("hiii"); 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.println("What is Your Name?"); 
     String name = br.readLine(); 
     System.out.println("Hello "+name+".."); 
     String hellorep = br.readLine(); 
     if(hellorep .equals(hel)) 
     { 
      System.out.println("Hello there..."); 
     } 
     else if(hellorep .equals(hel1)) 
     { 
      System.out.println("Hello there..."); 
     } 
     else if(hellorep .equals(hel2)) 
     { 
      System.out.println("Hello there..."); 
     } 
     else if(hellorep .equals(hel3)) 
     { 
      System.out.println("Hii"); 
     } 
     else if(hellorep .equals(hel4)) 
     { 
      System.out.println("Hiii"); 
     } 
     else if(hellorep .equals(hel5)) 
     { 
      System.out.println("Hii"); 
     } 
     else if(hellorep .equals(hel6)) 
     { 
      System.out.println("Hiii"); 
     } 
     else 
     { 
      System.out.println("Please type Again I Didn't get you.."); 
     } 
    } 
} 

回答

0

如果你想用一個窗口,使這個你可以用一個JTextField和的ActionListener添加到它,如果你想與控制檯使這一然後用輸入

那麼得到的消息又名其存儲在一個變量,然後做你的respond方法

if(STRINGVARIABLENAME.contains("Your word that you want it to check if it contains")) { then make the bot respond by Console or just do JTEXTFIELDNAME.setText("ANSWER HERE") }

,然後通過執行其他添加儘可能多的響應可能性,如果以後如果這樣

if(){ } else if(STRINGVARIABLENAME.contains("")){ }

,只要你想,有你有你的聊天機器人,你可以這樣做,因爲很多次:d

+0

和多數民衆贊成這樣做的一個簡單的方法,而不使代碼看起來像廢話 – Hydrox