数字模式(Number pattern using recursion)。

huangapple go评论59阅读模式
英文:

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 &lt;= 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&lt;=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 + &quot; &quot;);
        if(num1 &lt;= 0)
        {
           if (num1 == baseVal)
               return;
         }
           else
         {
           printNumPattern(num1 + num2, num2);
         }
        }
        else
        {
         printNumPattern(num1 - num2, num2);
         System.out.print(num1 + &quot; &quot;);
         }

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")

huangapple
  • 本文由 发表于 2020年9月12日 07:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63855337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定