我这里遇到了一些问题。我试图使用递归反转用户输入的字符串,但我不断收到错误。基本上,应该调用无参数 void 递归函数来向后打印字符串。我仅在反向词下的第 23 行收到错误,该错误指出“RecursionReversal 类型中的方法 reverse() 不适用于参数 (String)”。 Eclipse 中提供了对此问题的快速修复,但没有一个是我需要的。我错过了什么吗?
import java.util.Scanner;
public class RecursionReversal
{
public static String origChars;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter 5 characters:");
origChars= input.nextLine();
reverse();
}
public static void reverse()
{
if(origChars.length() <= 1)
return;
else
reverse(origChars.substring(1) + origChars.charAt(0));
}
}
最佳答案
向 reverse
方法添加一个参数,以对应于方法内进行的方法调用
public static void reverse(String input)
关于java - 使用递归反转字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26983457/