【问题标题】:How can I print a String in reverse without local variables one character at a time如何在没有局部变量的情况下反向打印字符串,一次一个字符
【发布时间】:2015-01-07 05:53:15
【问题描述】:

我必须创建一个接收字符串的递归方法。它不能在这个方法中存储任何字符串。

public static String printBackwards(String s)
{

    if (s.length() == 0) 
    {
        return s;
    }
    else
    {
        return printBackwards(s.substring(1)) + s.charAt(0);
    }
}

这就是我目前所拥有的

例如我输入了一个字符串,上面写着“你好”它返回到终端 o l l e h

【问题讨论】:

  • “一次返回字符串 1 个字符”是什么意思?你所拥有的似乎有效;我不明白你需要它做什么而不是做什么。
  • 您能否给我们一些示例输入和预期输出?正如 ajb 已经提到的那样,我不太确定一次一个字符是什么意思。
  • 您可以返回一个字符串,也可以返回一个字符,但不能一次返回一个字符。那没有意义。一个方法只返回一件事,而且只返回一次。
  • @JBNizet 好吧,您可能可以通过定义Iterator 一次返回一个字符String。不过,我有点怀疑这就是要求。另外,一想到要写一个递归迭代器就让我头疼。
  • 关于您的编辑:如果您只想在字符之间添加额外的空格,那么您应该能够通过在某处连接空格来修复您的代码。

标签: java string recursion char


【解决方案1】:

如果我正确理解您的问题,您希望方法本身一次打印一个字符。在这种情况下,返回类型是void

public static void printBackwards(String s) {
    if (s.length() != 0) {
        printBackwards(s.substring(1));
        System.out.println(s.charAt(0));
    }
}

【讨论】:

  • SO 是否有一个 ESP 徽章,用于弄清楚问题的含义,即使它表述得不好?如果是这样,我提名你。
【解决方案2】:

这应该可行。但是,我在 cmets 中内置了一些... ...轻微的错误,以防止您简单地复制和粘贴它。你最好在上交之前找到它们——而且你不敢在没有 cmets 的情况下上交 ;)

注意:这不会保留前导空格。您可能希望将此添加为练习。

import static java.lang.System.out;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Reverse {

  private static void printBackwards(String line) {

    if (line.length() == 0) {
      /* We are at the end of the line, we can continue */
      out.println();
      return;
    }
    /* We print the _last_ character of the string.
     * Remember, indices start at 0! */
    out.print(line.charAt(line.length() - 1));

    /* We reverse the part of the string we haven't printed */
    printBackwards(line.substring(0, line.length() - 1));
  }

  public static void main(String[] args) {

    /* If we don't have anything to input, no action is needed */
    if(args.length > 0) {

      /* Kind of a bonus: Any number of files can be processed */
      for(int a = 0; a<= args.length-1; a++){

        /* We need to construct a Path, since BufferedReader requires one */
        Path path = FileSystems.getDefault().getPath(args[a]);

        /* We do not initialize, as we want line to be null if the line.read() fails */
        String line;

        try {

          /* We construct a BufferedReader, which is pretty fast even for large text files */
          BufferedReader reader = Files.newBufferedReader(path, Charset.forName("UTF-8"));

          /* Next, we read each line... */
          while ((line = reader.readLine()) != null ) {
            /* ... and have it reversed */
            printBackwards(line);
          }
        } catch (IOException ex) {
          out.print("Something went wrong during line read or during creation of BufferedReader");
          ex.printStackTrace();
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 2023-03-18
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2011-06-29
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多