我有以下代码用于正在进行的聊天机器人工作。但是,它不能按预期工作。当我运行调试器时,它显示“再见”,由于一些奇怪的原因,不断被分配到“关键字”。从逻辑上讲,一切看起来应该按预期工作。但显然不是。任何帮助表示赞赏。我是Java编程的新手。
sum = 0, list = grid.getListofDots()
1. Find nearest dot from starting position (or previous dot that was removed) using manhattan distance
2. add to sum
3. Remove dot from list of possible dots
4. repeat steps 1 - 3 until list is empty
5. return the sum
答案 0 :(得分:1)
1,请阅读substring
的javadoc。
返回一个新字符串,该字符串是此字符串的子字符串。子串 从指定的beginIndex开始并延伸到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
类。
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!" );
}
}
答案 1 :(得分:0)
您可以尝试将“单词”初始化为字符串单词
示例
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!" );
}
这是我的猜测......