我想返回我的数组的值加上递归调用的返回值。
但是,出于某种原因,Java 不希望在构造函数之后有方法名。
此外,当我尝试将方法转换为另一种方法时,使用 isPalindrome 时出现错误。
我改变了我的程序,但我仍然遇到错误。
public class isPalindrome {
/**
* This is the main entry point for the application
* @return
*/
public static boolean main(String[] args) {
String[] word = {"KayaK", "Desserts, I stressed"};
boolean isPalindrome(String[] array, String s, String I) {
if(i.charAt(0) == s.charAt(0)) {
System.out.println("You entered a Palindrome");
return true;
}
else {
System.out.println("You didn't entered a Palindrome");
}
}
try {
System.in.read();
} catch (Throwable t) {
}
}
}
原文由 lonesarah 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不能将类名用作方法名。唯一可以与类共享名称的“方法”是构造函数。
一种解决方法是将您的课程从
isPalindrome
重命名为PalindromeFinder
或其他名称。这也将更好地符合 Java 命名约定。编辑:请注意,您实际上从未在 main 中调用过您的方法;您试图将局部变量分配给
isPalindrome
。这实际上并没有调用该方法。您需要使用isPalindrome(...put your parameters here...)
调用该方法,并将结果存储在一个名称未被使用的变量中。另请注意,方法只能返回单个值(单个基元或单个对象)。如果你真的想返回一个数组和一个布尔值(我不确定你是否这样做),你必须将它们存储在一个对象中并返回该对象。