【发布时间】:2013-03-02 04:26:04
【问题描述】:
import java.util.*;
public class RecursionProject {
public static void main(String[]args) {
getLine();
useRecursion();
}
public static void getLine() {
System.out.println("This program uses recursion.") ;
System.out.println("Would you like to see how it works?") ;
System.out.print("If yes, type yes, else type no -----> ");
String userResponse = null;
Scanner in = new Scanner(System.in);
userResponse = in.next();
System.out.println(userResponse);
if (userResponse.equalsIgnoreCase("yes")) {
System.out.println() ;
}
else {
System.out.println("Thank you for using this program.");
System.exit(0);
}
}
private static void useRecursion(){
System.out.println("Type in what you would like to see") ;
System.out.println("done recursively. (This program ") ;
System.out.println("excludes white spaces):") ;
String s = null ;
Scanner console = new Scanner(System.in) ;
s = console.next() ;
if (s.isEmpty()) {
System.out.print(" - ") ;
}
else {
System.out.println("0") ;
}
}
}
到目前为止,这是我的代码。我的任务是从控制台读取输入,然后使用递归反转相位。即,如果用户键入“动物”,它将在屏幕上打印出“slamina”。 我知道我的基本情况是该行是否为空,而我的递归情况是该行是否包含文本。 这是一个 Programming 2 类,在 Eclipse 4.2.2 上使用 Java
【问题讨论】:
-
是的...我们不能做你的作业...
-
我正在寻找一些建议。我的方向正确吗?
-
我在这里看不到任何实际的逻辑 - 你在哪里尝试编写递归代码?你能告诉我们你的尝试吗?
-
老实说。我完全迷路了。我已经阅读了关于递归的教科书大约 3 次,但仍然不知道该往哪个方向发展。我能解释一下如何在字符串上使用递归吗?
-
希望我的回答能对此有所启发。我认为您需要了解的关键是您正在通过重复使用子问题的结果来解决问题,直到最终子问题变得微不足道。例如,“Hello”的反义词就是“ello”的反义词,末尾有一个“H”。一旦你开始反转“o”,它就变得微不足道了。