Numerical recursive sequence in Java

Asked

Viewed 578 times

0

I need to do a numerical sequence in recursive mode that, after arriving at the n number (5, in the example), starts to do the inverse sequence.

Example:

1234554321

I’ve done what the normal sequence does, now there’s what turns the numbers around. You can help me?

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        System.out.println("Sequencia numerica...\n");
        int sequencia = nSequencia(1);
    }

    static int nSequencia(int iSequencia) {
        System.out.print(iSequencia);
        if(iSequencia >= 5){
            return 1;
        }
        else{
            return nSequencia(iSequencia + 1);
        }
    }  
}
  • This sounds like a bad statement, or bad recursion problem. Are you sure you should always go up to 5? then why are you talking about n?

  • You could use pointers to sort your list, thus making code more dynamic and not as static as the one you posted

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.