英文:
How do I count up using recursion in Java?
问题
所以,我正在参加EdX上的Java课程,到目前为止一直进展得很顺利。我现在正在学习递归,其中一个任务是创建一个方法,该方法计算从1到指定数字之间的数,并在每个数字之间插入逗号(例如,System.out.println(writeNum(5)); 应该打印出 "1, 2, 3, 4, 5, ... n",在最后一个数字后没有逗号)。
另外,如果传递了小于1的值,应该抛出IllegalArgumentException异常。
我已经花了2天时间,费尽心思,但我甚至无法理解如何开始。我可以做阶乘这个例子:
public static int factorial (int n) {
if(n == 1){
return 1;
}
System.out.println(n);
return n*factorial(n-1);
}
没有问题,对吧?所以,我一直在思考,基本情况是什么?是1吗?如果是,那么我只是在倒数,然后需要倒数,重新组织它们,重新打印它们,然后以某种方式找出解决方法。或者,我将基本情况设置为n == n,这也不起作用...我知道我可能想得太多了,但我甚至不知道如何开始...如果有人能逐步与我一起解决这个问题,帮助我理解,我会非常感激,因为我正在学习和理解这些内容。
英文:
So, I'm auditing a Java course on EdX and have been doing really well up until this point. I've come to recursions and one of the tasks is to create a method that counts up to a specified number placing commas between each (i.e. System.out.println(writeNum(5)); should print "1, 2, 3, 4, 5, ... n" with no comma on the final number).
Additionally, there's supposed to be an IllegalArgumentException should a value less than 1 be passed.
I've been on it for 2 days wracking my brain and I cannot even comprehend how to start. I can do the factorial one :
public static int factorial (int n) {
if(n == 1){
return 1;
}
System.out.println(n);
return n*factorial(n-1);
}
No problems right? So, thinking about it I keep thinking, what is the base case? Is it 1? if so, then I'm just counting down and somehow need to count down, reorganize them, reprint them, then somehow figure it out that way. Or, I make my base case when n==n, which doesn't work either... I know I am probably overthinking this, but I have no idea how to even get it started... If anyone could go through it with me step by step to understand this, I would sincerely be grateful as I'm doing this because I genuinely want to learn and understand this stuff.
答案1
得分: 0
对于递归,你需要找到何时返回,例如在下面给出的代码中,当 n == 1
时,该方法会打印 n
的值并返回。除了终止条件之外,另一个重要的方面是在何时(即在调用方法/函数进行递归之前还是之后)处理(例如打印)参数。
public class Main {
public static void main(String[] args) {
count(5);
}
static void count(int n) {
if (n == 1) {
System.out.print(n);
return;
}
count(n - 1);
System.out.print("," + n);
}
}
输出结果:
1,2,3,4,5
工作原理如下:
count(5) -> 调用 count(4),在 count(4) 返回后剩余的工作是打印 ,5
count(4) -> 调用 count(3),在 count(3) 返回后剩余的工作是打印 ,4
count(3) -> 调用 count(2),在 count(2) 返回后剩余的工作是打印 ,3
count(2) -> 调用 count(1),在 count(1) 返回后剩余的工作是打印 ,2
count(1) -> 打印 1 并返回
count(2) 剩余的工作完成,即打印 ,2
count(3) 剩余的工作完成,即打印 ,3
count(4) 剩余的工作完成,即打印 ,4
count(5) 剩余的工作完成,即打印 ,5
查看 此链接 以了解更多关于递归的信息。
英文:
For recursion, you need to find when to return e.g. in the code given below, when n == 1
, the method prints the value of n
and returns. Apart from the terminating condition, another important aspect is where (i.e. whether before calling the method/function recursively or after it) you process (e.g. print) the parameter.
public class Main {
public static void main(String[] args) {
count(5);
}
static void count(int n) {
if (n == 1) {
System.out.print(n);
return;
}
count(n - 1);
System.out.print("," + n);
}
}
Output:
1,2,3,4,5
This is how it works:
count(5) -> calls count(4) with remaining thing to do is print ,5 once count(4) returns.
count(4) -> calls count(3) with remaining thing to do is print ,4 once count(3) returns.
count(3) -> calls count(2) with remaining thing to do is print ,3 once count(2) returns.
count(2) -> calls count(1) with remaining thing to do is print ,2 once count(1) returns.
count(1) -> prints 1 and returns.
The remaining thing of count(2) is done i.e. ,2 is printed.
The remaining thing of count(3) is done i.e. ,3 is printed.
The remaining thing of count(4) is done i.e. ,4 is printed.
The remaining thing of count(5) is done i.e. ,5 is printed.
Check this to learn more about recursion.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论