我怎样才能把这个聊天机器人变成一个扫描用户输入的数组
How can i turn this chatter bot into an array that scans user input
嘿,基本上我有一个任务要做一个简单的聊天机器人,他的程序的目的是让用户输入一个带有 JOptionpane
的字符串,然后程序将搜索用户输入的 see如果他们写的任何内容包含我指定的关键字,如果是这样,他们的程序将显示特定消息。到目前为止,我使用 if-else 语句编写它,但老师希望我们使用数组(我不知道它们是如何工作的,我们只是希望知道)
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");
}
}
网上有很多关于 java 数组的优秀教程。此外,如果您的 class 遵循任何类型的教科书,那么它也应该包含数组。 https://www.tutorialspoint.com/java/java_arrays.html
只是来自快速 google.
一般来说,数组是一种数据结构,它像函数一样将对象保存在列表中。
一般来说
type[] var = new type[size];
或
type[] var = {foo0, foo1, foo2...};
实例
int[] intergerArray = new int[10];
String[] stringArray = {"Hello", "World"};
一般索引
var = 数组变量
index = 对象的位置 - 1(计算机从 0 开始)
var[index]
将 return 存储在 var
数组
位置 index
的值
索引示例
制作数组:
String[] stringArray = {"This", "is", "my", "first", "array"};
访问第一个值:
stringArray[0];
将值存储在变量中:
String firstWord = stringArray[0];
您甚至可以一次遍历整个数组:
for (int i = 0; i < stringArray.length; i++){
System.out.print(stringArray[i]);
}
输出:
This is my first array
为您的代码
我建议将您可能的输入放在一个数组中(甚至几个数组中)
String[] subjects = {"English", "Science", "Maths"};
然后您可以接受来自用户的输入并循环遍历您的数组以查看它是否与您支持的输入之一匹配。此外,通常您希望为无效输入包含一个 'default' 案例。
可能的实现
import javax.swing.JOptionPane;
public class ChatterBot{
public static void main(String[] args){
String[] subjects = {"English", "Maths", "Science"};
String userInput = JOptionPane
.showInputDialog("Pick one of the subjects listed to learn a fun fact (english, science, maths) ");
if (userInput.contains(subjects[0]){
// english facts
} else if (userInput.contains(subjects[1]){
// science facts
} else if (userInput.contains(subjects[2])){
// maths facts
}
}
}
嘿,基本上我有一个任务要做一个简单的聊天机器人,他的程序的目的是让用户输入一个带有 JOptionpane
的字符串,然后程序将搜索用户输入的 see如果他们写的任何内容包含我指定的关键字,如果是这样,他们的程序将显示特定消息。到目前为止,我使用 if-else 语句编写它,但老师希望我们使用数组(我不知道它们是如何工作的,我们只是希望知道)
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");
}
}
网上有很多关于 java 数组的优秀教程。此外,如果您的 class 遵循任何类型的教科书,那么它也应该包含数组。 https://www.tutorialspoint.com/java/java_arrays.html
只是来自快速 google.
一般来说,数组是一种数据结构,它像函数一样将对象保存在列表中。
一般来说
type[] var = new type[size];
或
type[] var = {foo0, foo1, foo2...};
实例
int[] intergerArray = new int[10];
String[] stringArray = {"Hello", "World"};
一般索引
var = 数组变量
index = 对象的位置 - 1(计算机从 0 开始)
var[index]
将 return 存储在 var
数组
index
的值
索引示例
制作数组:
String[] stringArray = {"This", "is", "my", "first", "array"};
访问第一个值:
stringArray[0];
将值存储在变量中:
String firstWord = stringArray[0];
您甚至可以一次遍历整个数组:
for (int i = 0; i < stringArray.length; i++){
System.out.print(stringArray[i]);
}
输出:
This is my first array
为您的代码
我建议将您可能的输入放在一个数组中(甚至几个数组中)
String[] subjects = {"English", "Science", "Maths"};
然后您可以接受来自用户的输入并循环遍历您的数组以查看它是否与您支持的输入之一匹配。此外,通常您希望为无效输入包含一个 'default' 案例。
可能的实现
import javax.swing.JOptionPane;
public class ChatterBot{
public static void main(String[] args){
String[] subjects = {"English", "Maths", "Science"};
String userInput = JOptionPane
.showInputDialog("Pick one of the subjects listed to learn a fun fact (english, science, maths) ");
if (userInput.contains(subjects[0]){
// english facts
} else if (userInput.contains(subjects[1]){
// science facts
} else if (userInput.contains(subjects[2])){
// maths facts
}
}
}