【问题标题】:Searching Muiltple arrays for keywords Java在多个数组中搜索关键字 Java
【发布时间】:2018-04-05 03:36:49
【问题描述】:

我是一个非常业余的 Java 编码器,试图为 Uni 作业创建一个聊天机器人。我希望程序搜索我的动物数组,如果找到关键字,我可以使用数组的名称来给出特定的响应。到目前为止,我有两个问题(我想还会有更多问题)。

第一个-我的匹配系统仅在输入对话框中输入的唯一单词时才会识别关键字。例如,我只能说“狗”或“狗”,但我喜欢说“我想谈谈狗”,它会在句子中找到关键字。

第二个问题,如果关键字在第一个数组中没有找到,为什么不移动下一个数组?

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Chatbot {

    public static void main(String[] args) {

    String animal1[] = {"Dogs","Dawgs","dogs"};
    String animal2[] = {"Cats"};
    String animal3[] = {"Horses"};
    String animal4[] = {"Lamas"};
    String animal5[] = {"Ducks"};
    String animal6[] = {"Pigs"};
    String animal7[] = {"Cows"};
    String animal8[] = {"Rabbits"};
    String animal9[] = {"Chickens"};
    String animal10[] = {"Mice"};


  String response = JOptionPane.showInputDialog("Hi there, theses are all the animals i like to talk about ! :"
                + "\n-Dogs"
                +"\n-Cats"
                +"\n-Rabbits"
                +"\n-Lamas"
                +"\n-Hores"
                +"\n-Ducks"
                +"\n-Pigs"
                +"\n-Cows"
                +"\n-Chickens"
                +"\n-Mice"
                +"\n-Ask me somthing !");       

  int  i;
  boolean found=false; {

  }

 //DOGS
  for(i=0; i<animal1.length;i++){
      found=true;
        // match your regex containing words from array against input
      if(response.matches(".*\\b"+animal1[i]+"\\b.*")){
  String dog = JOptionPane.showInputDialog("Dogs ? I love dogs, they taste great!\n" + "What other animals do you like ?");
  //CATS
      }
  else
      for(i=0; i<animal2.length;i++){
          found=true;
      if(response.matches(".*\\b"+animal2[i]+"\\b.*")){
  String cat = JOptionPane.showInputDialog("Cats ? I love cats, they taste great!\n" + "What other animals do you like ?");

      }
else
  if(response.matches(".*\\b"+animal3[i]+"\\b.*")){
String horse = JOptionPane.showInputDialog("Horses ? I love Horses, they taste great!\n" + "What other animals do you like ?");

  }     
   }        
    } 
     } 
    }

【问题讨论】:

  • String#contains 将帮助您解决匹配问题。至于你的第二个问题......我会使用一个多维数组,但在任何一种情况下,编写一个方法,它需要一个 String 数组(作为源)和一个要搜索的术语,这样你可以使用search(animal1, "dogs");search(animal2, "dogs"); ...等...

标签: java arrays for-loop java.util.scanner


【解决方案1】:

您可以使用多维数组来解决您的问题。并且可以通过“contains”和“toLowerCase”方法匹配字符串值。

public class ChatBot {

  public static void main(String[] args) {
    String[][] animalArray = new String[10][3];

    animalArray[0][0] = "Dogs";
    animalArray[0][1] = "Dawgs";

    animalArray[1][0] = "Cats";
    animalArray[1][1] = "Catds";

    animalArray[2][0] = "Horses";

    animalArray[3][0] = "Lamas";

    animalArray[4][0] = "Ducks";
    animalArray[4][1] = "Duckss";

    animalArray[5][0] = "Pigs";

    animalArray[6][0] = "Cows";

    animalArray[7][0] = "Rabbits";

    animalArray[8][0] = "Chickens";

    animalArray[9][0] = "Mice";


    String response = JOptionPane.showInputDialog("Hi there, theses are all the animals i like to talk about ! :"
            + "\n-Dogs" + "\n-Cats" + "\n-Rabbits" + "\n-Lamas" + "\n-Hores" + "\n-Ducks" + "\n-Pigs" + "\n-Cows"
            + "\n-Chickens" + "\n-Mice" + "\n-Ask me somthing !");

    while (true) {
        for (int i = 0; i < animalArray.length; i++) {
            for (int j = 0; j < animalArray[i].length; j++) {
                if (animalArray[i][j] != null && response.toLowerCase().contains(animalArray[i][j].toLowerCase())) {
                    String animal = animalArray[i][0];
                    response = JOptionPane.showInputDialog(animal + " ? I love " + animal + ", they taste great!\n"
                            + "What other animals do you like ?");
                    break;
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:
    public static void main(String[] args){
        animalsDialog(arr(), start());
    }
    public static String start(){
        String res = JOptionPane.showInputDialog("Dogs or Cats?");
        return res;
    }
    public static String[] arr(){
        String[] animals = new String[10];
        animals[0] = "Dogs";
        animals[1] = "dogs";
        animals[2] = "Cats";
        animals[3] = "cats";
        return animals;
    }
    public static void animalsDialog(String[] animals, String res){
        boolean search;
        String anim = "";
        for (String animal : animals) {
            search = animal.equalsIgnoreCase(res);
            if(search == true){
                anim = JOptionPane.showInputDialog("You like " + animal + " Great!" + 
                        " What other animals do you like ?");
                animalsDialog(animals, anim);
            } 
        }
    }
    

    【讨论】:

    • 这不是只使用一个数组吗?
    • 另外,在输入对话框中也找不到关键字
    猜你喜欢
    • 2015-04-02
    • 2012-07-24
    • 1970-01-01
    • 2021-03-13
    • 2019-04-16
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多