我怎么能把它变成一个数组

时间:2018-03-26 13:42:24

标签: java arrays eclipse loops

嘿所以基本上我被告知要为我的任务的第一部分编写一个非常基本的聊天机器人程序,对于第二部分,我现在需要创建一个包含我已编写的代码的数组,产生相同的结果,但我不知道怎么去吧。我看了很多在线视频,但他们只讨论int数组和我的项目我需要一个字符串数组,可以接收用户输入,扫描/搜索用户输入中的关键字然后显示" JOptionPane&#34 ;获取另一个用户输入并检查关键字等。 生病了我现在的代码:

enter code here      
import javax.swing.JOptionPane;

public class ChatterBot {

public static void main(String args[]) {
    String input = "";
    String maths = "";
    String science = "";
    String chemFact = "";
    String bioFact = "";
    String zooFact = "";
    String algFact = "";
    String yes = "Well good for you";
    String no = "You learn something new everyday :)";
    input = JOptionPane
            .showInputDialog("Pick one of the subjects listed to learn a fun fact (english, science, maths) ");

    if (input.contains("science")) {
        science = JOptionPane.showInputDialog(
                "What kind of science fact woukd you like to know about? (chem, Biology, Zoology)");
    }

    else if (input.contains("maths")) {
        maths = JOptionPane.showInputDialog(
                "What kind of maths fact would you like to know about? (algebra, fractions, division) ");
    }
    if (maths.contains("algebra")) {
        algFact = JOptionPane.showInputDialog(
                "\"Did you know a mathematician who specializes in algebra is called an algebraist? (yes or no)\"");
    }
    if (algFact.contains("yes")) {
        System.out.println(yes);
    } else if (algFact.contains("no")) {
        System.out.println(no);
    }

    if (science.contains("chem")) {
        chemFact = JOptionPane.showInputDialog(
                "Did you know If you pour a handful of salt into a full glass of water the water level will actually go down rather than overflowing the glass? (yes or no)");
    }

    if (chemFact.contains("yes")) {
        System.out.println(yes);
    } else if (chemFact.contains("no")) {
        System.out.println(no);
    }

    else if (science.contains("biology")) {
        bioFact = JOptionPane.showInputDialog("Did you know The brain itself cannot feel pain? (yes or no)");
    }

    if (bioFact.contains("yes")) {
        System.out.println("Well good for you");
    } else if (bioFact.contains("no")) {
        System.out.println("You learn something new everyday :)");
    }

    else if (science.contains("zoology")) {
        zooFact = JOptionPane
                .showInputDialog("Did you know butterflies have taste receptors on their feet? (yes or no)");
    }

    if (zooFact.contains("yes")) {
        System.out.println("Well good for you");
    } else if (zooFact.contains("no")) {
        System.out.println("You learn something new everyday :)");
    }
    if (input.contains("?")) {
        System.out.println("I will be asking the questions");
    }

}

}

1 个答案:

答案 0 :(得分:0)

如何使用字符串数组的示例是:

String[] facts = new String[]{"fact1", "fact2", "fact3"};

fact1 = facts[0];

System.out.println(fact1);

要将事实类型分开,您需要多个数组。

用于存储所有事实但是将每个事实类型分开的合适数据结构是Map。