Java 聊天机器人 -- 代码未按预期运行
Java chatbot -- code not working as intended
我正在为聊天机器人编写以下代码。但是,它无法按预期工作。当我 运行 调试器时,它显示 "bye",出于某种奇怪的原因,一直被分配给 "keyword"。从逻辑上讲,一切看起来都应该按预期工作。但显然不是。任何帮助表示赞赏。我是 Java 编程的新手。
public class Chatbot {
public Chatbot( )
{
}
/*
* Generates a variety of responses, based on what the user has stated
*/
public static void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( statement.length() == 0 )
{
System.out.println( "Please say something :)" );
}
else if( findKeyword( statement, "hi" ) > 0 ||
findKeyword( statement, "hello" ) > 0 ||
findKeyword( statement, "hey" ) > 0 ||
findKeyword( statement, "hiya" ) > 0 ||
findKeyword( statement, "heya" ) > 0 )
{
System.out.println( "Hello to you too!" );
}
else if( findKeyword( statement, "how are you" ) > 0 ||
findKeyword( statement, "hows it going" ) > 0 ||
findKeyword( statement, "howre you" ) > 0 ||
findKeyword( statement, "how ya doing" ) > 0 ||
findKeyword( statement, "yo wassup" ) > 0 ||
findKeyword( statement, "hey whats up" ) > 0 ||
findKeyword( statement, "whats up" ) > 0 )
{
System.out.println( "I'm good, how are you?" );
}
}
/*
* findKeyword method, returns either a 0 or a 1
* @ 0 -- keyword not found
* @ 1 -- keyword found
*/
public static int findKeyword( String statement, String keyword )
{
// This is in case the keyword is not in the statement at all
if( !statement.contains( keyword ) )
{
return 0;
}
int position = statement.toLowerCase().indexOf( keyword.toLowerCase() ); // position of the keyword in the statement
statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " "; // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword
String sub = statement.substring( position, position + keyword.length() + 1 ); // isolates the keyword with 1 character before and after
String charBeforeKeyword = sub.substring( 0, 1 ); // the character before the keyword
String charAfterKeyword = sub.substring( sub.length() - 1, sub.length() ); // the character after the keyword
/*
* Now, we check to see if the characters we isolated before are letters; if they are *
* @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
* @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
*/
if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
&& (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
{
return 1;
}
return 0;
}
}
import java.util.Scanner;
public class Execute
{
public static void main( String [] args )
{
// Variables and Objects
Chatbot bot = new Chatbot();
Scanner input = new Scanner( System.in );
String statement = "";
// Prompt and get the user's first input
System.out.println( "Type text to start chatting!" );
statement = input.nextLine();
// While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
while( bot.findKeyword( statement, "bye" ) != 1 &&
bot.findKeyword( statement, "cya" ) != 1 &&
bot.findKeyword( statement, "goodbye" ) != 1 &&
bot.findKeyword( statement, "gtg" ) != 1 )
{
bot.respond( statement );
statement = input.nextLine();
}
System.out.println( "Goodbye!" );
}
}
1,请阅读substring
.
的javadoc
Returns a new string that is a substring of this string. The substring
begins at the specified beginIndex and extends to the character at
index endIndex - 1
根据你的逻辑,你要修改
String sub = statement.substring( position, position + keyword.length() + 1 );
至
String sub = statement.substring( position, position + keyword.length() + 2 );
2、在Chatbot
中不需要使用静态方法,因为你已经创建了Chatbot
.
的实例
3、不用的时候记得关闭Scanner
查看更新后的代码如下:
public class Chatbot {
public Chatbot( )
{
}
/*
* Generates a variety of responses, based on what the user has stated
*/
public void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( statement.length() == 0 )
{
System.out.println( "Please say something :)" );
}
else if( findKeyword( statement, "hi" ) > 0 ||
findKeyword( statement, "hello" ) > 0 ||
findKeyword( statement, "hey" ) > 0 ||
findKeyword( statement, "hiya" ) > 0 ||
findKeyword( statement, "heya" ) > 0 )
{
System.out.println( "Hello to you too!" );
}
else if( findKeyword( statement, "how are you" ) > 0 ||
findKeyword( statement, "hows it going" ) > 0 ||
findKeyword( statement, "howre you" ) > 0 ||
findKeyword( statement, "how ya doing" ) > 0 ||
findKeyword( statement, "yo wassup" ) > 0 ||
findKeyword( statement, "hey whats up" ) > 0 ||
findKeyword( statement, "whats up" ) > 0 )
{
System.out.println( "I'm good, how are you?" );
}
}
/*
* findKeyword method, returns either a 0 or a 1
* @ 0 -- keyword not found
* @ 1 -- keyword found
*/
public int findKeyword( String statement, String keyword )
{
// This is in case the keyword is not in the statement at all
if( !statement.contains( keyword ) )
{
return 0;
}
int position = statement.toLowerCase().indexOf( keyword.toLowerCase() ); // position of the keyword in the statement
statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " "; // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword
String sub = statement.substring( position, position + keyword.length() + 2 ); // isolates the keyword with 1 character before and after
String charBeforeKeyword = sub.substring( 0, 1 ); // the character before the keyword
String charAfterKeyword = sub.substring( sub.length() - 1, sub.length()); // the character after the keyword
/*
* Now, we check to see if the characters we isolated before are letters; if they are *
* @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
* @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
*/
if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
&& (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
{
return 1;
}
return 0;
}
}
还有 Execute
class.
import java.util.Scanner;
public class Execute
{
public static void main( String [] args )
{
// Variables and Objects
Chatbot bot = new Chatbot();
Scanner input = new Scanner( System.in );
String statement = "";
// Prompt and get the user's first input
System.out.println( "Type text to start chatting!" );
statement = input.nextLine();
// While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
while( bot.findKeyword( statement, "bye" ) != 1 &&
bot.findKeyword( statement, "cya" ) != 1 &&
bot.findKeyword( statement, "goodbye" ) != 1 &&
bot.findKeyword( statement, "gtg" ) != 1 )
{
bot.respond( statement );
statement = input.nextLine();
}
input.close();
System.out.println( "Goodbye!" );
}
}
你可以试试把"words"初始化成一个String字
示例
public class Chatbot {
public Chatbot( )
{
String hi= "hi"
String hello= "Hello"
String hey= "Hey"
String hiya= "Hiya"
String heya= "Heya"
String n;
}
/*
* Generates a variety of responses, based on what the user has stated
*/
public static void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( n = sc1.next()== "")
{
System.out.println( "Please say something :)" );
}
else if( findKeyword( statement, hi ) ||
findKeyword( statement, hello ) ||
findKeyword( statement,hey ) ||
findKeyword( statement, hiya) ||
findKeyword( statement, heya) )
{
System.out.println( "Hello to you too!" );
}
不过这是我的猜测...
我正在为聊天机器人编写以下代码。但是,它无法按预期工作。当我 运行 调试器时,它显示 "bye",出于某种奇怪的原因,一直被分配给 "keyword"。从逻辑上讲,一切看起来都应该按预期工作。但显然不是。任何帮助表示赞赏。我是 Java 编程的新手。
public class Chatbot {
public Chatbot( )
{
}
/*
* Generates a variety of responses, based on what the user has stated
*/
public static void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( statement.length() == 0 )
{
System.out.println( "Please say something :)" );
}
else if( findKeyword( statement, "hi" ) > 0 ||
findKeyword( statement, "hello" ) > 0 ||
findKeyword( statement, "hey" ) > 0 ||
findKeyword( statement, "hiya" ) > 0 ||
findKeyword( statement, "heya" ) > 0 )
{
System.out.println( "Hello to you too!" );
}
else if( findKeyword( statement, "how are you" ) > 0 ||
findKeyword( statement, "hows it going" ) > 0 ||
findKeyword( statement, "howre you" ) > 0 ||
findKeyword( statement, "how ya doing" ) > 0 ||
findKeyword( statement, "yo wassup" ) > 0 ||
findKeyword( statement, "hey whats up" ) > 0 ||
findKeyword( statement, "whats up" ) > 0 )
{
System.out.println( "I'm good, how are you?" );
}
}
/*
* findKeyword method, returns either a 0 or a 1
* @ 0 -- keyword not found
* @ 1 -- keyword found
*/
public static int findKeyword( String statement, String keyword )
{
// This is in case the keyword is not in the statement at all
if( !statement.contains( keyword ) )
{
return 0;
}
int position = statement.toLowerCase().indexOf( keyword.toLowerCase() ); // position of the keyword in the statement
statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " "; // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword
String sub = statement.substring( position, position + keyword.length() + 1 ); // isolates the keyword with 1 character before and after
String charBeforeKeyword = sub.substring( 0, 1 ); // the character before the keyword
String charAfterKeyword = sub.substring( sub.length() - 1, sub.length() ); // the character after the keyword
/*
* Now, we check to see if the characters we isolated before are letters; if they are *
* @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
* @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
*/
if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
&& (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
{
return 1;
}
return 0;
}
}
import java.util.Scanner;
public class Execute
{
public static void main( String [] args )
{
// Variables and Objects
Chatbot bot = new Chatbot();
Scanner input = new Scanner( System.in );
String statement = "";
// Prompt and get the user's first input
System.out.println( "Type text to start chatting!" );
statement = input.nextLine();
// While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
while( bot.findKeyword( statement, "bye" ) != 1 &&
bot.findKeyword( statement, "cya" ) != 1 &&
bot.findKeyword( statement, "goodbye" ) != 1 &&
bot.findKeyword( statement, "gtg" ) != 1 )
{
bot.respond( statement );
statement = input.nextLine();
}
System.out.println( "Goodbye!" );
}
}
1,请阅读substring
.
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1
根据你的逻辑,你要修改
String sub = statement.substring( position, position + keyword.length() + 1 );
至
String sub = statement.substring( position, position + keyword.length() + 2 );
2、在Chatbot
中不需要使用静态方法,因为你已经创建了Chatbot
.
3、不用的时候记得关闭Scanner
查看更新后的代码如下:
public class Chatbot {
public Chatbot( )
{
}
/*
* Generates a variety of responses, based on what the user has stated
*/
public void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( statement.length() == 0 )
{
System.out.println( "Please say something :)" );
}
else if( findKeyword( statement, "hi" ) > 0 ||
findKeyword( statement, "hello" ) > 0 ||
findKeyword( statement, "hey" ) > 0 ||
findKeyword( statement, "hiya" ) > 0 ||
findKeyword( statement, "heya" ) > 0 )
{
System.out.println( "Hello to you too!" );
}
else if( findKeyword( statement, "how are you" ) > 0 ||
findKeyword( statement, "hows it going" ) > 0 ||
findKeyword( statement, "howre you" ) > 0 ||
findKeyword( statement, "how ya doing" ) > 0 ||
findKeyword( statement, "yo wassup" ) > 0 ||
findKeyword( statement, "hey whats up" ) > 0 ||
findKeyword( statement, "whats up" ) > 0 )
{
System.out.println( "I'm good, how are you?" );
}
}
/*
* findKeyword method, returns either a 0 or a 1
* @ 0 -- keyword not found
* @ 1 -- keyword found
*/
public int findKeyword( String statement, String keyword )
{
// This is in case the keyword is not in the statement at all
if( !statement.contains( keyword ) )
{
return 0;
}
int position = statement.toLowerCase().indexOf( keyword.toLowerCase() ); // position of the keyword in the statement
statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " "; // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword
String sub = statement.substring( position, position + keyword.length() + 2 ); // isolates the keyword with 1 character before and after
String charBeforeKeyword = sub.substring( 0, 1 ); // the character before the keyword
String charAfterKeyword = sub.substring( sub.length() - 1, sub.length()); // the character after the keyword
/*
* Now, we check to see if the characters we isolated before are letters; if they are *
* @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
* @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
*/
if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
&& (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
{
return 1;
}
return 0;
}
}
还有 Execute
class.
import java.util.Scanner;
public class Execute
{
public static void main( String [] args )
{
// Variables and Objects
Chatbot bot = new Chatbot();
Scanner input = new Scanner( System.in );
String statement = "";
// Prompt and get the user's first input
System.out.println( "Type text to start chatting!" );
statement = input.nextLine();
// While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
while( bot.findKeyword( statement, "bye" ) != 1 &&
bot.findKeyword( statement, "cya" ) != 1 &&
bot.findKeyword( statement, "goodbye" ) != 1 &&
bot.findKeyword( statement, "gtg" ) != 1 )
{
bot.respond( statement );
statement = input.nextLine();
}
input.close();
System.out.println( "Goodbye!" );
}
}
你可以试试把"words"初始化成一个String字
示例
public class Chatbot {
public Chatbot( )
{
String hi= "hi"
String hello= "Hello"
String hey= "Hey"
String hiya= "Hiya"
String heya= "Heya"
String n;
}
/*
* Generates a variety of responses, based on what the user has stated
*/
public static void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( n = sc1.next()== "")
{
System.out.println( "Please say something :)" );
}
else if( findKeyword( statement, hi ) ||
findKeyword( statement, hello ) ||
findKeyword( statement,hey ) ||
findKeyword( statement, hiya) ||
findKeyword( statement, heya) )
{
System.out.println( "Hello to you too!" );
}
不过这是我的猜测...