英文:
Number pattern using recursion
问题
private static void printNumPattern(int num1, int num2) {
int temp = num1;
System.out.println(num1);
if (num1 <= 0) {
if (num1 == temp) {
System.out.println();
} else {
printNumPattern(num1 + num2, num2);
}
} else {
printNumPattern(num1 - num2, num2);
}
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1;
int num2;
num1 = scnr.nextInt();
num2 = scnr.nextInt();
printNumPattern(num1, num2);
}
英文:
I am having a bit of trouble with recursion, my first input will be the starting number and it will keep being subtracted by the second input. Ex) 12 and 3 as inputs and the output is 12 9 6 3 0 3 6 9 12. The printNumPattern method is the recursion method.
private static void printNumPattern(int num1, int num2) {
int temp = num1;
System.out.println(num1);
if(num1 <= 0) {
if(num1 == temp) {
System.out.println();
}else {
printNumPattern(num1+num2,num2);
}
}else {
printNumPattern(num1-num2,num2);
}
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num1;
int num2;
num1 = scnr.nextInt();
num2 = scnr.nextInt();
printNumPattern(num1, num2);
}
答案1
得分: 2
你可以在递归调用之后进行操作,在没有任何计算的情况下重用 n1:
static void p(int n1, int n2) {
System.out.println(n1);
if (n1 <= 0) return;
p(n1 - n2, n2);
System.out.println(n1);
}
(我在手机上输入,所以将名称缩短了一些)
英文:
You can do things after the recursive call, reusing n1 without any calculation:
static void p(int n1,int n2){
System.out.println(n1);
if(n1<=0)return;
p(n1-n2,n2);
System.out.println(n1);
}
(I'm typing on phone, that's why I shortened names a little)
答案2
得分: 1
对于你的printNumPattern
的每次运行,所有的变量都只属于那次运行。这适用于int temp
,还适用于int num1, int num2
(你的参数)。
因此,在第2行中,你说int temp = num1;
。如果num1
为0或更小,那么你接着执行if (num1 == temp)
,这将始终为真。else
部分(跳转到printNumPattern(num1+num2,num2);
)永远不会执行。
看起来你想要temp
保持为最初的输入。如果是这样的话,在你的printNumPattern
方法中继续将它作为第三个参数传递。
英文:
For every 'run' of your printNumPattern
, all the variables are unique to just that one run. This goes for int temp
, and also for int num1, int num2
(your parameters).
So, in line 2 you say int temp = num1;
. If num1
is 0 or below, you then do if (num1 == temp)
, which will always be true. The else
(that goes to printNumPattern(num1+num2,num2);
cannot possibly ever execute.
It sounds like you want temp
to be the original input. If you want that, keep passing it along in your printNumPattern
method as third parameter.
答案3
得分: 0
一些程序可能会对代码产生问题。作者离最近。
int baseVal = num1;
System.out.print(num1 + " ");
if(num1 <= 0)
{
if (num1 == baseVal)
return;
}
else
{
printNumPattern(num1 + num2, num2);
}
}
else
{
printNumPattern(num1 - num2, num2);
System.out.print(num1 + " ");
}
这对我使用的任何程序都有效。zybooks、VS和notepad++。如果是zybooks,程序希望输出时不要有换行,所以没有ln或f("\n")。
英文:
Some programs may give issues with the code. The author was closest.
int baseVal = num1;
System.out.print(num1 + " ");
if(num1 <= 0)
{
if (num1 == baseVal)
return;
}
else
{
printNumPattern(num1 + num2, num2);
}
}
else
{
printNumPattern(num1 - num2, num2);
System.out.print(num1 + " ");
}
This works on any program I used. zybooks, VS, and notepad++. If its zybooks, the program wants an output without line breaks so thats why there is no ln or f("\n")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论