我需要编写一个将字符串作为参数并以镜像形式打印单词的方法。例如,“hello”应该返回“helloolleh”。我必须使用递归并且不能使用 for 循环。到目前为止,这是我的代码:
public static String printMirrored(String str)
{
if(str == null || str.equals(""))
{
return str;
}
else
{
return str + printMirrored(str.substring(1)) + str.charAt(0);
}
}
我的输出是“helloellollolooolleh”,其中显然有一些额外的东西。任何指针将不胜感激!