java - 使用递归反转字符串

标签 java string recursion input reverse

我这里遇到了一些问题。我试图使用递归反转用户输入的字符串,但我不断收到错误。基本上,应该调用无参数 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/

相关文章:

java - 检查字符串是否为回文(使用方法)

c++ - 使用模板和字符串?

python - 按降序将两个排序列表合并为一个

c++ - 递归模板函数在初始化时导致负数组大小

在 fragment 中使用 Butterknife 时出现 java.lang.NullPointerException

java - thymeleaf 电子邮件模板背景图像未显示

python - 我如何在整个文本文件中搜索一行文本

Java拦截HTTP请求

Java:空比较总是产生错误:不正确的警告?

sql - 同一张表上的postgres递归查询