在java中,如何通过检测到某个词来设置一个动作发生?
In java, how can I set an action to happen by detecting a certain word?
所以对于我的代码,我有一个基本的 "chatbot" 和 class 生成一个非常基本的面孔(面部可以微笑或皱眉或表现得无聊)最后我有一个 driver class.
我的问题是:我是否有可能获得代码来生成 smile/frown/ 当它检测到某个单词(在一个短语中)时感到无聊
这是我目前拥有的:
聊天机器人class:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
JPanel p = new JPanel();
JTextArea dialog = new JTextArea(20,50);
JTextArea input = new JTextArea(1,50);
JScrollPane scroll = new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
String[][] chatBot={
//standard greetings
{"hi","hello","hola","ola","howdy" , "wassup", "sup", "bonjour", },
{"hi","hello","hey"},
//question greetings
{"how are you","how r you","how r u","how are u, how are you?, how are you ?", "wassup"},
{"good","doing well", "not bad", "meh", "I could be better", "However you want me to be "},
//yes
{"yes"},
{"no"},
//default
{"how was your day?"}
};
public static void main(String[] args){
new ChatBot();
}
public ChatBot(Changingface x){
super("Chat Bot");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
input.addKeyListener(this);
p.add(scroll);
p.add(input);
p.setBackground(new Color(75,14,215));
add(p);
setVisible(true);
x= new ChangingFace();//need help with this, teacher told me to make a parameter for the constructor and then make code that
//was able to detect a word in a phrase and respond accordingly to that word/phrase
if(input.getText("day")){//this is how far I got
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//-----grab quote-----------
String quote=input.getText();
input.setText("");
addText("-->You:\t"+quote);
quote.trim();
while(
quote.charAt(quote.length()-1)=='!' ||
quote.charAt(quote.length()-1)=='.' ||
quote.charAt(quote.length()-1)=='?'// how are you? how are you ?
){
quote=quote.substring(0,quote.length()-1);
quote.trim();
}
byte response=0;
/*
0:we're searching through chatBot[][] for matches
1:we didn't find anything
2:we did find something
*/
//-----check for matches----
int j=0;//which group we're checking
while(response==0){
if(inArray(quote.toLowerCase(),chatBot[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
addText("\n-->Chatty McChatface:\t"+chatBot[(j*2)+1][r]);
}
j++;
if(j*2==chatBot.length-1 && response==0){
response=1;
}
}
//-----default--------------
if(response==1){
int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
addText("\n-->Chatty McChatface:\t"+chatBot[chatBot.length-1][r]);
}
addText("\n");
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
public void keyTyped(KeyEvent e){}
public void addText(String str){
dialog.setText(dialog.getText()+str);
}
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
之后就是我的脸class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChangingFace extends JFrame implements ActionListener
{
int whichFeel = 0;
private JButton happyButton = new JButton("Cheeky Smirk");
private JButton thinkButton = new JButton("Bored");
private JButton sadButton = new JButton("Somethin fishy");
public ChangingFace()
{
// set the title
setTitle("Face behind the ChatBot");
// choose a Flow Layout policy
setLayout(new FlowLayout());
// add the buttons to the frame
add(happyButton);
add(thinkButton);
add(sadButton);
// set the background to cyan
getContentPane().setBackground(Color.cyan);
// enable the buttons to listen for a mouse-click
happyButton.addActionListener(this);
thinkButton.addActionListener(this);
sadButton.addActionListener(this);
// configure the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(720, 420);//250,200
setLocation(100,100);//300,300
setVisible(true);
}
public void paint(Graphics g)
{
// call the paint method of the superclass, Jframe
super.paint(g);
// paint the face
g.setColor(Color.red);//red yellow blue orange pink cyan magenta black white gray lightGray darkGray
//http://mathbits.com/MathBits/Java/Graphics/Color.htm
g.drawOval(85,75,75,75);//85,75,75,75
g.setColor(Color.darkGray);
g.fillOval(100,95,10,10);//100,95,10,10
g.fillOval(135,95,10,10);//135,95,10,10
g.drawString("Savage OGB lgt", 79,181);
g.drawRect(121, 150, 3, 20);//121,150,3,20
if(whichFeel == 1)
{
// draw a smiling mouth
g.drawArc(102,115,40,25,0,-180);
}
else if(whichFeel == 3)
{
// draw a frowning mouth
g.drawArc(102,115,40,25,0,180);
}
else if(whichFeel == 2)
{
// draw a thinking mouth
g.drawLine(102, 130, 142, 130);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == happyButton)
{
whichFeel = 1;
repaint();
}
if(e.getSource() == thinkButton)
{
whichFeel = 2;
repaint();
}
if(e.getSource() == sadButton)
{
whichFeel = 3;
repaint();
}
}
}
和 driver class:
public class ChangingFaceTester{
public static void main(String[] args){
new ChangingFace();
new ChatBot();
}
}
我的问题又来了:如何通过从用户输入中检测到 phrase/word 来让脸 smile/frown/思考(表现无聊)?
您可以使用String.contains()方法。
if(input.getText().contains("beer"){
x.setWhichFeel(1); // x is the variable name you gave your ChangingFace
} // instance in the chatbot class
编辑:
public class ChangingFace(){
private int whichFeel;
public void setWhichFeel(int num){
whichFeel = num;
}
}
可以从 ChatBot class 调用此方法以更改 ChangeFace class 中的 whichFeel 变量。
编辑 2:
ChatBot 的构造函数需要一个参数,一个 ChangingFace 对象:
public ChatBot(Changingface x){
}
在你的主要方法中你有:
public static void main(String[] args){
new ChatBox();
}
由于 ChatBot 需要一个参数,因此您应该传入一个 ChangingFace 对象。
public static void main(String[] args){
new ChatBox(new ChangingFace());
}
您还提到您的驱动程序 class 有一个 main 方法。应该只有一种主要方法。它在哪个 class 中并不重要,但请记住您的 chatBox 对象必须接受 ChangingFace 对象作为参数。
编辑 3:
经过进一步审查,将主要方法保留在 ChangingFaceTester class 中,但其结构如下:
public class ChangingFaceTester{
public static void main(String[] args){
new ChatBot(new ChangingFace());
}
}
所以对于我的代码,我有一个基本的 "chatbot" 和 class 生成一个非常基本的面孔(面部可以微笑或皱眉或表现得无聊)最后我有一个 driver class.
我的问题是:我是否有可能获得代码来生成 smile/frown/ 当它检测到某个单词(在一个短语中)时感到无聊
这是我目前拥有的:
聊天机器人class:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
JPanel p = new JPanel();
JTextArea dialog = new JTextArea(20,50);
JTextArea input = new JTextArea(1,50);
JScrollPane scroll = new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
String[][] chatBot={
//standard greetings
{"hi","hello","hola","ola","howdy" , "wassup", "sup", "bonjour", },
{"hi","hello","hey"},
//question greetings
{"how are you","how r you","how r u","how are u, how are you?, how are you ?", "wassup"},
{"good","doing well", "not bad", "meh", "I could be better", "However you want me to be "},
//yes
{"yes"},
{"no"},
//default
{"how was your day?"}
};
public static void main(String[] args){
new ChatBot();
}
public ChatBot(Changingface x){
super("Chat Bot");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
input.addKeyListener(this);
p.add(scroll);
p.add(input);
p.setBackground(new Color(75,14,215));
add(p);
setVisible(true);
x= new ChangingFace();//need help with this, teacher told me to make a parameter for the constructor and then make code that
//was able to detect a word in a phrase and respond accordingly to that word/phrase
if(input.getText("day")){//this is how far I got
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//-----grab quote-----------
String quote=input.getText();
input.setText("");
addText("-->You:\t"+quote);
quote.trim();
while(
quote.charAt(quote.length()-1)=='!' ||
quote.charAt(quote.length()-1)=='.' ||
quote.charAt(quote.length()-1)=='?'// how are you? how are you ?
){
quote=quote.substring(0,quote.length()-1);
quote.trim();
}
byte response=0;
/*
0:we're searching through chatBot[][] for matches
1:we didn't find anything
2:we did find something
*/
//-----check for matches----
int j=0;//which group we're checking
while(response==0){
if(inArray(quote.toLowerCase(),chatBot[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
addText("\n-->Chatty McChatface:\t"+chatBot[(j*2)+1][r]);
}
j++;
if(j*2==chatBot.length-1 && response==0){
response=1;
}
}
//-----default--------------
if(response==1){
int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
addText("\n-->Chatty McChatface:\t"+chatBot[chatBot.length-1][r]);
}
addText("\n");
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
public void keyTyped(KeyEvent e){}
public void addText(String str){
dialog.setText(dialog.getText()+str);
}
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
之后就是我的脸class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChangingFace extends JFrame implements ActionListener
{
int whichFeel = 0;
private JButton happyButton = new JButton("Cheeky Smirk");
private JButton thinkButton = new JButton("Bored");
private JButton sadButton = new JButton("Somethin fishy");
public ChangingFace()
{
// set the title
setTitle("Face behind the ChatBot");
// choose a Flow Layout policy
setLayout(new FlowLayout());
// add the buttons to the frame
add(happyButton);
add(thinkButton);
add(sadButton);
// set the background to cyan
getContentPane().setBackground(Color.cyan);
// enable the buttons to listen for a mouse-click
happyButton.addActionListener(this);
thinkButton.addActionListener(this);
sadButton.addActionListener(this);
// configure the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(720, 420);//250,200
setLocation(100,100);//300,300
setVisible(true);
}
public void paint(Graphics g)
{
// call the paint method of the superclass, Jframe
super.paint(g);
// paint the face
g.setColor(Color.red);//red yellow blue orange pink cyan magenta black white gray lightGray darkGray
//http://mathbits.com/MathBits/Java/Graphics/Color.htm
g.drawOval(85,75,75,75);//85,75,75,75
g.setColor(Color.darkGray);
g.fillOval(100,95,10,10);//100,95,10,10
g.fillOval(135,95,10,10);//135,95,10,10
g.drawString("Savage OGB lgt", 79,181);
g.drawRect(121, 150, 3, 20);//121,150,3,20
if(whichFeel == 1)
{
// draw a smiling mouth
g.drawArc(102,115,40,25,0,-180);
}
else if(whichFeel == 3)
{
// draw a frowning mouth
g.drawArc(102,115,40,25,0,180);
}
else if(whichFeel == 2)
{
// draw a thinking mouth
g.drawLine(102, 130, 142, 130);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == happyButton)
{
whichFeel = 1;
repaint();
}
if(e.getSource() == thinkButton)
{
whichFeel = 2;
repaint();
}
if(e.getSource() == sadButton)
{
whichFeel = 3;
repaint();
}
}
}
和 driver class:
public class ChangingFaceTester{
public static void main(String[] args){
new ChangingFace();
new ChatBot();
}
}
我的问题又来了:如何通过从用户输入中检测到 phrase/word 来让脸 smile/frown/思考(表现无聊)?
您可以使用String.contains()方法。
if(input.getText().contains("beer"){
x.setWhichFeel(1); // x is the variable name you gave your ChangingFace
} // instance in the chatbot class
编辑:
public class ChangingFace(){
private int whichFeel;
public void setWhichFeel(int num){
whichFeel = num;
}
}
可以从 ChatBot class 调用此方法以更改 ChangeFace class 中的 whichFeel 变量。
编辑 2:
ChatBot 的构造函数需要一个参数,一个 ChangingFace 对象:
public ChatBot(Changingface x){
}
在你的主要方法中你有:
public static void main(String[] args){
new ChatBox();
}
由于 ChatBot 需要一个参数,因此您应该传入一个 ChangingFace 对象。
public static void main(String[] args){
new ChatBox(new ChangingFace());
}
您还提到您的驱动程序 class 有一个 main 方法。应该只有一种主要方法。它在哪个 class 中并不重要,但请记住您的 chatBox 对象必须接受 ChangingFace 对象作为参数。
编辑 3:
经过进一步审查,将主要方法保留在 ChangingFaceTester class 中,但其结构如下:
public class ChangingFaceTester{
public static void main(String[] args){
new ChatBot(new ChangingFace());
}
}