【问题标题】:How can i turn this into an array我怎样才能把它变成一个数组
【发布时间】:2018-03-26 13:42:23
【问题描述】:

嘿,所以基本上我被告知要为我的作业的第一部分编写一个非常基本的聊天机器人程序,而对于第二部分,我现在需要使用我已经编写的代码创建一个数组,产生相同的结果,但我不知道如何去做。 Iv 在网上看了很多视频,但他们只谈论 int 数组,对于我的项目,我需要一个可以接受用户输入的字符串数组,扫描/搜索用户输入中的关键字,然后显示“JOptionPane”以获取另一个用户输入和检查关键字等。 我会告诉你我当前的代码:

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");
    }

}

}

【问题讨论】:

  • 您创建一个与 int 数组相同的字符串数组,但使用 String 作为类型。不清楚具体是什么问题。
  • scanner 在这里查看扫描器类,您也可以使用它来扫描输入的关键字。

标签: java arrays eclipse loops


【解决方案1】:

如何使用字符串数组的一个例子是:

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

fact1 = facts[0];

System.out.println(fact1);

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

一个适合存储所有事实的数据结构,但保持每个事实类型分开的是一个 Map。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2018-03-26
    • 2021-12-13
    • 1970-01-01
    • 2019-07-12
    相关资源
    最近更新 更多