【发布时间】:2025-12-24 17:15:06
【问题描述】:
我对 Java 很陌生,我正在尝试制作一个聊天机器人。我有一个主要方法,然后是一个带有响应的方法。虽然当我尝试在 main 方法中调用 response 方法时,它突出显示了 Response 一词并说:类 ChatCode 中的方法 Response 不能应用于给定类型; 必需:java.lang.String;发现:没有参数;原因:实际参数列表和形式参数列表的长度不同
public class ChatCode{
public static String main(String input){
Scanner sc = new Scanner(System.in);
System.out.println("Hello!");
input = sc.nextLine();
while(input != ("Bye")){
Response();
}
System.out.println("Bye!");
}
那么这是我的回应方式
public static String Response(String output){
Scanner sc = new Scanner(System.in);
input = input.toLowerCase();
input = input.trim();
String output;
if(input.indexOf("hi!") >= 0
|| ("hi") >= 0
|| ("hello!") >= 0
|| ("hello") >= 0){
output = "Hello!";
}
else if(input.indexOf("whats up?") >= 0
|| ("what's up?") >= 0
|| ("whats up") >= 0
|| ("what's up") >= 0){
output = "Nothing much, how about you?";
}
else if(input.indexOf("how are you?") >= 0
|| ("how are you") >= 0){
output = "I am great, how about you?";
}
return output;
}
任何反馈将不胜感激!!!!
【问题讨论】:
-
(0)
input != ("Bye")-> How do I compare strings in Java?; (1)Response(String output){需要参数,所以你不能通过Response()调用它; (2) 方法名应以小写开头。 -
您的方法
public static String Response(String output){被声明为需要String参数,但您使用的是Response();,这不是一回事,您必须将其传递给null或@ 987654331@值 -
这里需要传入一个String,while(input != ("Bye")){ Response(); // 像这样 -> Response(input); }