1
$\begingroup$

Can anyone help with this question? I currently have this solution that prints

1 2 3 4 5 6

but I'm having trouble going back down to 1.

public class noLoop{

     public static void main(String []args){
        noLoop(6);
     }

     public static void noLoop(int n) {

         if (n == 0) {
             return;
         }

         else {
             noLoop(n-1);
             System.out.println(n);
         }
     }
}
$\endgroup$
2
  • 3
    $\begingroup$ Can’t you just print the string 1 2 3 4 5 6 5 4 3 2 1? $\endgroup$ Dec 18, 2018 at 6:34
  • 2
    $\begingroup$ This is a Java programming question, hence it is off-topic here on CS.SE. Also, the programming-languages tag is not appropriate for programming questions. Community: please, let's avoid answering off-topic questions. $\endgroup$
    – chi
    Dec 18, 2018 at 10:54

3 Answers 3

0
$\begingroup$

As I understand, this problem is NOT about just printing "1 2 3 4 5 6 6 5 4 3 2 1" or "1 2 3 4 5 6 5 4 3 2 1". The number 6 is just to give an example of an integer.

Since noLoop is not successful, it is only natural to use a second recursion method. So noLoop and noLoop2 will do the job.

If you have to use one recursion method, an extra parameter such as StringBuilder result might be needed to store what we got so far. That is how noLoop3 comes. (Another way is to use a static variable instead of the extra parameter. I will let you to figure that out.)

public class NoLoop {
    public static void main(String[] args) {
        noLoop(6);
        noLoop2(6 - 1);

        StringBuilder sb = new StringBuilder();
        noLoop3(6, sb);
        System.out.println(sb);
    }

    public static void noLoop(int n) {
        if (n > 0) {
            noLoop(n - 1);
            System.out.println(n);
        }
    }

    public static void noLoop2(int n) {
        if (n > 0) {
            System.out.println(n);
            noLoop2(n - 1);
        }
    }

    public static void noLoop3(int n, StringBuilder result ) {
        if (n > 0) {
            result.insert(0, n).append(n);
            noLoop3(n - 1, result);
        }
    }
}

By the way, this question borders on programming, which is off-topic here. Please consider posting this kind of question to Stackoverflow later on.

$\endgroup$
3
$\begingroup$
public class Foo {
    public static void main(String[] java_is_silly) {
        System.out.print("1 2 3 4 5 6 5 4 3 2 1");
    }
}
$\endgroup$
-1
$\begingroup$

This is clearly a Computer Science problem to demonstrate functional programming and recursion. It needs action on the head and tail of recursion and a middle exit.

Those who only program without applying Computer Science would not approach it that way and would get poor marks from me!

// Recursion demonstration in Java
class NoLoop
{
    public static void main(String [] args) 
    {
        doit(1);
        System.out.println();
    }

    static void doit(int i)
    {
      System.out.print(i + " ");
      if (i >= 6) return;
      doit(i+1);
      System.out.print(i + " ");
    }
}
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.