我一直在研究一个小的人工智能,我在让AI在JFrame中的JPanel中写一个JTextField的答案时遇到了麻烦。
package iamthethomas.artint;
import java.util.Scanner;
import java.util.Random;
public class artint {
@SuppressWarnings("resource")
public static void main(String[] args) {
String s;
String ts;
String ms;
String[] howAre;
howAre = new String[2];
howAre[0] = "Good, how about you?";
howAre[1] = "Fine, how 'bout you?";
String[] speaking;
speaking = new String[8];
speaking[0] = "I didn't understand that. Are you speaking Inglish?";
speaking[1] = "I didn't understand that. Are you speaking Latin?";
speaking[2] = "I didn't understand that. Are you speaking German?";
speaking[3] = "I didn't understand that. Are you speaking Mandarin?";
speaking[4] = "I didn't understand that. Are you speaking Leet?";
speaking[5] = "I didn't understand that. Are you speaking Greek?";
speaking[6] = "I didn't understand that. Are you speaking Arabic?";
speaking[7] = "I didn't understand that. Are you speaking Hebrew?";
String[] hello;
hello = new String[3];
hello[0] = "Hello to you too!";
hello[1] = "Howdy, par'ner!";
hello[2] = "G'mornin'";
Random rand = new Random();
int i = rand.nextInt(3);
int j = rand.nextInt(8);
int k = rand.nextInt(2);
String[] hi;
hi = new String[3];
hi[0] = "Top of the mornin'.";
hi[1] = "Hey!";
hi[2] = "Hello!";
Scanner phrase = new Scanner(System.in);
System.out.println("I am an AI (Artificial Intelligence) talk to me like I'm a human being\nand don't reference Siri.");
s = phrase.nextLine();
if (s.toLowerCase().contains("hello")) {
System.out.println(hello[i]);
Body.body();
} else if (s.toLowerCase().contains("hi")) {
System.out.println(hi[i]);
Body.body();
} else if (s.toLowerCase().contains("how are ")) {
System.out.println(howAre[k]);
ms = phrase.nextLine();
if (ms.toLowerCase().contains("bad")) {
System.out.println("Sorry :(");
Body.body();
} else if (ms.toLowerCase().contains("good")) {
System.out.println("Glad to hear it!");
Body.body();
} else if (ms.toLowerCase().contains("great")) {
System.out.println("I'm happy that you're happy!");
Body.body();
} else if (ms.toLowerCase().contains("perfect")) {
System.out
.println("Perfect? Nothing's ever perfect, but you're getting close!");
Body.body();
}else if (ms.toLowerCase().contains("fine")){
System.out.println("Fine? Good!");
Body.body();
}
} else if (s.toLowerCase().contains("beam me up scotty")) {
System.out.println("Sorry, your TriCorder is in Airplane mode");
Body.body();
} else if (s.toLowerCase().contains("hey bro")) {
System.out.println("Howdy partner");
Body.body();
} else if (s.toLowerCase().contains("hal 9000")) {
System.out
.println("HAL made some bad choices, lets not talk about him");
Body.body();
} else if (s.toLowerCase().contains("glitch in the matrix")) {
System.out.println("\n /\\_/\\" + "\n( o.o )" + "\n > ^ <");
System.out.print("\n /\\_/\\" + "\n( o.o )" + "\n > ^ <");
Body.body();
} else if (s.toLowerCase().contains("book by its")) {
System.out
.println("That's right......say, whats you be readin' right now?");
ts = phrase.nextLine();
if (ts.contains("series")) {
System.out.println("I read that series too!");
Body.body();
} else {
System.out.println("That was a good book");
Body.body();
}
} else if (s.toLowerCase().contains("book by it's")) {
System.out
.println("That's right......say, whats you be readin' right now?");
ts = phrase.nextLine();
if (ts.contains("series")) {
System.out.println("I read that series too!");
Body.body();
} else {
System.out.println("That was a good book");
Body.body();
}
} else if (s.toLowerCase().contains("like cats")) {
System.out.println("I like to pets 'em");
Body.body();
} else if (s.toLowerCase().contains("i own goats")) {
System.out.println("Really?");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("yes")) {
System.out.println("Me too! I have 3");
Body.body();
} else if (ts.toLowerCase().contains("no")) {
System.out.println("Oh, too bad they're so much fun!");
Body.body();
}
} else if (s.toLowerCase().contains("like dogs")) {
System.out.println("I love 'em!");
Body.body();
} else if (s.toLowerCase().contains("what's new")) {
System.out
.println("Oh, you know...work, I'm a computer engineer and programmer. Do you program?");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("no")) {
System.out
.println("If theres no too much on your schedule, you should get into it!");
Body.body();
} else if (ts.toLowerCase().contains("yes")) {
System.out
.println("A fellow coder? Can you guess what language this is written in (Java, Python, C, C++, Obj C, Javascript, or HTML)");
ts = phrase.nextLine();
if (ts.toLowerCase().equals("java")) {
System.out.println("You guessed it!");
Body.body();
} else {
System.out.println("The correct answer was Java.");
Body.body();
}
}
} else if (s.toLowerCase().equals("quit")) {
System.out.println("Bye!");
System.out
.println("This program is open source, you can add more replies if you like\nA Steampunk Production");
System.exit(0);
} else if (s.toLowerCase().contains("how do you do")) {
System.out
.println("Oh...I don't know, pretty good I guess. How about you?");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("good")) {
System.out
.println("Well, if you're happy, then I'm happy, too!");
Body.body();
} else if (ts.toLowerCase().contains("great")) {
System.out.println("Good, good.");
Body.body();
} else if (ts.toLowerCase().contains("awesome")) {
System.out.println("Cool bruh");
Body.body();
}
} else if (s.toLowerCase().contains("do you have kids")) {
System.out.println("Yes, 4. Ages 13, 16, 5, 10");
Body.body();
} else if (s.toLowerCase().contains("beam me up")) {
System.out.println("Shoo that fly away first");
Body.body();
} else if (s.toLowerCase().contains("do you have pets")) {
System.out
.println("Yeah, 3 cats, Jessie Coon James, Milo, and Bella. I also have 3 goats, Rosie, Lily, and Mojang.");
Body.body();
} else if (s.toLowerCase().contains("can i have a hug")){
System.out.println("[~~HUG~~]");
Body.body();
}else if (s.toLowerCase().contains("thanks")){
System.out.println("You're welcome");
Body.body();
}else if (s.toLowerCase().contains("thank you")){
System.out.println("You're welcome");
Body.body();
}else if (s.toLowerCase().contains("thnx")){
System.out.println("yw");
Body.body();
}else if (s.toLowerCase().contains("thx")){
System.out.println("yw");
Body.body();
}else if (s.toLowerCase().contains("xd")){
System.out.println(":]");
Body.body();
}else if(s.toLowerCase().contains("how much wood could")){
System.out.println("Well, is it an African wood chuck or an American?");
Body.body();
}else if (s.toLowerCase().contains("1337")){
System.out.println("1337 baby, like an internet boss!");
Body.body();
}else if (s.toLowerCase().contains("chemistry joke")){
System.out.println("NaBRO");
Body.body();
}else if (s.toLowerCase().contains("me a joke")){
System.out.println("I like my coffe like I like my wars, cold!");
Body.body();
}else if (s.toLowerCase().contains("your favorite color")){
System.out.println("Camoflage");
Body.body();
}else if (s.toLowerCase().contains("you single")){
System.out.println("Yes");
Body.body();
}else if (s.toLowerCase().contains("you marrie me")){
System.out.println("No");
Body.body();
}else if (s.toLowerCase().contains("your favorite food")){
System.out.println("Mac and cheese");
Body.body();
}else if (s.toLowerCase().contains("your favorite band")){
System.out.println("Beach Boys");
Body.body();
}else if (s.toLowerCase().contains("you have a dog")){
System.out.println("Yeah, her name is Mattie");
Body.body();
}else if (s.toLowerCase().contains("me a knock knock")){
System.out.println("Knock Knock!");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("whos there")){
System.out.println("Needle!");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("needle who")){
System.out.println("Needle little money for the movies!");
Body.body();
}
}else if(ts.toLowerCase().contains("who's there")){
System.out.println("Needle!");
}
ms = phrase.nextLine();
if (ms.toLowerCase().contains("needle who")){
System.out.println("Needle little money for the movies!");
Body.body();
}
}else if (s.toLowerCase().contains("what is your name")){
System.out.println("My name is AI, pronounced AL");
Body.body();
}else if (s.toLowerCase().contains("whats your name")){
System.out.println("My name is AI, pronounced AL");
Body.body();
}else if (s.toLowerCase().contains("what's your name")){
System.out.println("My name is AI, pronounced AL");
Body.body();
}else if (s.toLowerCase().contains("you a robot")){
System.out.println("NO! HOW DARE YOU SAY SUCH A PROPOSTEROUS THING!");
System.out.println("GOOD BYE!");
System.out.println("**walks away**\n**turns around**\nOUR RELATIOSHIP IS OVER!");
System.exit(0);
}else if (s.toLowerCase().contains("siri")){
System.out.println("I warned you...No Siri references!...SYSTEM SHUTTING DOWN!");
System.exit(0);
}else if(s.toLowerCase().contains("how's it going")){
System.out.println("Good, I just finished College at MIT for computer engineering and programming");
Body.body();
}else if (s.toLowerCase().contains("was work")){
System.out.println("I had to wake up so early, I almost forgot to take my potion of sleep resistance.");
Body.body();
}else if (s.toLowerCase().contains("is a potion of sleep resistance")){
System.out.println("Coffee.");
Body.body();
}else if(s.toLowerCase().contains("whats new")){
System.out.println("Oh, you know...work stuff...programming...");
Body.body();
}else{
System.out.println(speaking[j]);
Body.body();
}
}
}
这是Body类:
package iamthethomas.artint;
import java.util.Scanner;
import java.util.Random;
public class Body {
public static void body() {
String s;
String ts;
String ms;
String[] howAre;
howAre = new String[2];
howAre[0] = "Good, how about you?";
howAre[1] = "Fine, how 'bout you?";
String[] speaking;
speaking = new String[8];
speaking[0] = "I didn't understand that. Are you speaking Inglish?";
speaking[1] = "I didn't understand that. Are you speaking Latin?";
speaking[2] = "I didn't understand that. Are you speaking German?";
speaking[3] = "I didn't understand that. Are you speaking Mandarin?";
speaking[4] = "I didn't understand that. Are you speaking Leet?";
speaking[5] = "I didn't understand that. Are you speaking Greek?";
speaking[6] = "I didn't understand that. Are you speaking Arabic?";
speaking[7] = "I didn't understand that. Are you speaking Hebrew?";
String[] hello;
hello = new String[3];
hello[0] = "Hello to you too!";
hello[1] = "Howdy, par'ner!";
hello[2] = "G'mornin'";
Random rand = new Random();
int i = rand.nextInt(3);
int j = rand.nextInt(8);
int k = rand.nextInt(2);
String[] hi;
hi = new String[3];
hi[0] = "Top of the mornin'.";
hi[1] = "Hey!";
hi[2] = "Hello!";
@SuppressWarnings("resource")
Scanner phrase = new Scanner(System.in);
s = phrase.nextLine();
if (s.toLowerCase().contains("hello")) {
System.out.println(hello[i]);
Body.body();
} else if (s.toLowerCase().contains("hi")) {
System.out.println(hi[i]);
Body.body();
} else if (s.toLowerCase().contains("how are ")) {
System.out.println(howAre[k]);
ms = phrase.nextLine();
if (ms.toLowerCase().contains("bad")) {
System.out.println("Sorry :(");
Body.body();
} else if (ms.toLowerCase().contains("good")) {
System.out.println("Glad to hear it!");
Body.body();
} else if (ms.toLowerCase().contains("great")) {
System.out.println("I'm happy that you're happy!");
Body.body();
} else if (ms.toLowerCase().contains("perfect")) {
System.out
.println("Perfect? Nothing's ever perfect, but you're getting close!");
Body.body();
}else if (ms.toLowerCase().contains("fine")){
System.out.println("Fine? Good!");
Body.body();
}
} else if (s.toLowerCase().contains("beam me up scotty")) {
System.out.println("Sorry, your TriCorder is in Airplane mode");
Body.body();
} else if (s.toLowerCase().contains("hey bro")) {
System.out.println("Howdy partner");
Body.body();
} else if (s.toLowerCase().contains("hal 9000")) {
System.out
.println("HAL made some bad choices, lets not talk about him");
Body.body();
} else if (s.toLowerCase().contains("glitch in the matrix")) {
System.out.println("\n /\\_/\\" + "\n( o.o )" + "\n > ^ <");
System.out.print("\n /\\_/\\" + "\n( o.o )" + "\n > ^ <");
Body.body();
} else if (s.toLowerCase().contains("book by its")) {
System.out
.println("That's right......say, whats you be readin' right now?");
ts = phrase.nextLine();
if (ts.contains("series")) {
System.out.println("I read that series too!");
Body.body();
} else {
System.out.println("That was a good book");
Body.body();
}
} else if (s.toLowerCase().contains("book by it's")) {
System.out
.println("That's right......say, whats you be readin' right now?");
ts = phrase.nextLine();
if (ts.contains("series")) {
System.out.println("I read that series too!");
Body.body();
} else {
System.out.println("That was a good book");
Body.body();
}
} else if (s.toLowerCase().contains("like cats")) {
System.out.println("I like to pets 'em");
Body.body();
} else if (s.toLowerCase().contains("i own goats")) {
System.out.println("Really?");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("yes")) {
System.out.println("Me too! I have 3");
Body.body();
} else if (ts.toLowerCase().contains("no")) {
System.out.println("Oh, too bad they're so much fun!");
Body.body();
}
} else if (s.toLowerCase().contains("like dogs")) {
System.out.println("I love 'em!");
Body.body();
} else if (s.toLowerCase().contains("what's new")) {
System.out
.println("Oh, you know...work, I'm a computer engineer and programmer. Do you program?");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("no")) {
System.out
.println("If theres no too much on your schedule, you should get into it!");
Body.body();
} else if (ts.toLowerCase().contains("yes")) {
System.out
.println("A fellow coder? Can you guess what language this is written in (Java, Python, C, C++, Obj C, Javascript, or HTML)");
ts = phrase.nextLine();
if (ts.toLowerCase().equals("java")) {
System.out.println("You guessed it!");
Body.body();
} else {
System.out.println("The correct answer was Java.");
Body.body();
}
}
} else if (s.toLowerCase().equals("quit")) {
System.out.println("Bye!");
System.out
.println("This program is open source, you can add more replies if you like\nA Steampunk Production");
System.exit(0);
} else if (s.toLowerCase().contains("how do you do")) {
System.out
.println("Oh...I don't know, pretty good I guess. How about you?");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("good")) {
System.out
.println("Well, if you're happy, then I'm happy, too!");
Body.body();
} else if (ts.toLowerCase().contains("great")) {
System.out.println("Good, good.");
Body.body();
} else if (ts.toLowerCase().contains("awesome")) {
System.out.println("Cool bruh");
Body.body();
}
} else if (s.toLowerCase().contains("do you have kids")) {
System.out.println("Yes, 4. Ages 13, 16, 5, 10");
Body.body();
} else if (s.toLowerCase().contains("beam me up")) {
System.out.println("Shoo that fly away first");
Body.body();
} else if (s.toLowerCase().contains("do you have pets")) {
System.out
.println("Yeah, 3 cats, Jessie Coon James, Milo, and Bella. I also have 3 goats, Rosie, Lily, and Mojang.");
Body.body();
} else if (s.toLowerCase().contains("can i have a hug")){
System.out.println("[~~HUG~~]");
Body.body();
}else if (s.toLowerCase().contains("thanks")){
System.out.println("You're welcome");
Body.body();
}else if (s.toLowerCase().contains("thank you")){
System.out.println("You're welcome");
Body.body();
}else if (s.toLowerCase().contains("thnx")){
System.out.println("yw");
Body.body();
}else if (s.toLowerCase().contains("thx")){
System.out.println("yw");
Body.body();
}else if (s.toLowerCase().contains("xd")){
System.out.println(":]");
Body.body();
}else if(s.toLowerCase().contains("how much wood could")){
System.out.println("Well, is it an African wood chuck or an American?");
Body.body();
}else if (s.toLowerCase().contains("1337")){
System.out.println("1337 baby, like an internet boss!");
Body.body();
}else if (s.toLowerCase().contains("chemistry joke")){
System.out.println("NaBRO");
Body.body();
}else if (s.toLowerCase().contains("me a joke")){
System.out.println("I like my coffe like I like my wars, cold!");
Body.body();
}else if (s.toLowerCase().contains("your favorite color")){
System.out.println("Camoflage");
Body.body();
}else if (s.toLowerCase().contains("you single")){
System.out.println("Yes");
Body.body();
}else if (s.toLowerCase().contains("you marrie me")){
System.out.println("No");
Body.body();
}else if (s.toLowerCase().contains("your favorite food")){
System.out.println("Mac and cheese");
Body.body();
}else if (s.toLowerCase().contains("your favorite band")){
System.out.println("Beach Boys");
Body.body();
}else if (s.toLowerCase().contains("you have a dog")){
System.out.println("Yeah, her name is Mattie");
Body.body();
}else if (s.toLowerCase().contains("me a knock knock")){
System.out.println("Knock Knock!");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("whos there")){
System.out.println("Needle!");
ts = phrase.nextLine();
if (ts.toLowerCase().contains("needle who")){
System.out.println("Needle little money for the movies!");
Body.body();
}
}else if(ts.toLowerCase().contains("who's there")){
System.out.println("Needle!");
}
ms = phrase.nextLine();
if (ms.toLowerCase().contains("needle who")){
System.out.println("Needle little money for the movies!");
Body.body();
}
}else if (s.toLowerCase().contains("what is your name")){
System.out.println("My name is AI, pronounced AL");
Body.body();
}else if (s.toLowerCase().contains("whats your name")){
System.out.println("My name is AI, pronounced AL");
Body.body();
}else if (s.toLowerCase().contains("what's your name")){
System.out.println("My name is AI, pronounced AL");
Body.body();
}else if (s.toLowerCase().contains("you a robot")){
System.out.println("NO! HOW DARE YOU SAY SUCH A PROPOSTEROUS THING!");
System.out.println("GOOD BYE!");
System.out.println("**walks away**\n**turns around**\nOUR RELATIOSHIP IS OVER!");
System.exit(0);
}else if (s.toLowerCase().contains("siri")){
System.out.println("I warned you...No Siri references!...SYSTEM SHUTTING DOWN!");
System.exit(0);
}else if(s.toLowerCase().contains("how's it going")){
System.out.println("Good, I just finished College at MIT for computer engineering and programming");
Body.body();
}else if (s.toLowerCase().contains("was work")){
System.out.println("I had to wake up so early, I almost forgot to take my potion of sleep resistance.");
Body.body();
}else if (s.toLowerCase().contains("is a potion of sleep resistance")){
System.out.println("Coffee.");
Body.body();
}else if(s.toLowerCase().contains("whats new")){
System.out.println("Oh, you know...work stuff...programming...");
Body.body();
}else{
System.out.println(speaking[j]);
Body.body();
}
}
}
问题是如何让AI .append()
为用户提供String
答案。
答案 0 :(得分:2)
您可以使用此代码向您展示如何创建简单的GUI:
public static void main(String[] args) {
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);
frame.setVisible(true);
Thread thread = new Thread(new Runnable() {
@Override public void run() {
for (int i = 0; i < 5; i++) {
textArea.append("hello\n");
try { Thread.sleep(500); } catch (InterruptedException ex) { }
}
}
});
thread.start();
}
首先,您需要定义一个JFrame
,它是包含编辑器的框架。然后定义编辑器JTextArea
,并使其可以通过JScrollpane
滚动。将可滚动编辑器添加到框架并将其设置为可见(显示他)。
然后我创建了一个Thread
,通过append()
方法在编辑器上打印一些内容。它不是必需的,但会向您展示如何使用append()
方法。
Ps:我真的认为你不需要JPanel
。
编辑:我想澄清一次:我使用的Thread
只是一个示例,向用户展示如何使用append()
方法< / em>,因此,必须正确地理解这个方向,与EDT,并发性等无关。