Java 如何在没有局部变量的情况下反向打印字符串,一次打印一个字符
我必须创建一个接收字符串的递归方法。它不能在此方法中存储任何字符串Java 如何在没有局部变量的情况下反向打印字符串,一次打印一个字符,java,string,recursion,char,Java,String,Recursion,Char,我必须创建一个接收字符串的递归方法。它不能在此方法中存储任何字符串 public static String printBackwards(String s) { if (s.length() == 0) { return s; } else { return printBackwards(s.substring(1)) + s.charAt(0); } } 这就是我目前所拥有的 示例我输入一个表示“Hello
public static String printBackwards(String s)
{
if (s.length() == 0)
{
return s;
}
else
{
return printBackwards(s.substring(1)) + s.charAt(0);
}
}
这就是我目前所拥有的
示例我输入一个表示“Hello”的字符串,它返回到终端o l e h如果我正确理解了您的问题,您希望方法本身一次打印一个字符。在这种情况下,返回类型为
void
public static void printBackwards(String s) {
if (s.length() != 0) {
printBackwards(s.substring(1));
System.out.println(s.charAt(0));
}
}
这应该行得通。然而,我已经在一些。。。注释中的细微错误会阻止您简单地复制和粘贴它。你最好在交上来之前找到他们——而且你不敢在没有评论的情况下交上来;) 注意:这不会保留前导空格。您可能希望将此添加为练习
导入静态java.lang.System.out;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.nio.charset.charset;
导入java.nio.file.FileSystems;
导入java.nio.file.Files;
导入java.nio.file.Path;
公共类反向{
私有静态void向后打印(字符串行){
if(line.length()==0){
/*我们已经到了终点,可以继续了*/
out.println();
返回;
}
/*我们打印字符串的最后一个字符。
*记住,索引从0开始*/
打印输出(line.charAt(line.length()-1));
/*我们把没有打印的部分倒过来*/
向后打印(line.substring(0,line.length()-1));
}
公共静态void main(字符串[]args){
/*如果我们没有任何需要输入的内容,就不需要采取任何行动*/
如果(args.length>0){
/*一种额外的好处:可以处理任意数量的文件*/
对于(int a=0;a“一次返回字符串1个字符”是什么意思?你所做的似乎有效;我不明白你需要它做什么,而它没有做什么。你能给我们一些输入和预期输出的例子吗?正如ajb已经提到的,我不太确定你一次只返回一个字符是什么意思。你可以返回一个字符串,也可以返回一个字符,但你不能一次返回一个字符。这是没有意义的。一个方法只返回一件事,而且只返回一次。@jbnize不太好,通过定义一个迭代器,你可能一次返回一个字符串一个字符。不过,我有点怀疑这是什么要求。另外,一想到编写一个递归迭代器,我就大吃一惊。关于你r编辑:如果您只想在字符之间加上额外的空格,那么您应该能够通过在某个地方连接空格来修复代码。这样做是否有一个ESP徽章,即使在问题陈述不当的情况下,也能找出问题的含义?如果是,我推荐您。
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();
}
}
}
}
}