Counting numbers up and down using Recursion

后端 未结 3 1534
一个人的身影
一个人的身影 2021-01-23 22:12

Given two numbers, let\'s say start = 1 and end = 4, I am trying to count all the numbers in sequence up and then down . No looping is allowed

3条回答
  •  醉话见心
    2021-01-23 22:34

    try this

     private static int  CountUpAndDown(int end, int first, int start)
        {
            if(end==first)
            {
                return -1;
            }
            if (start > end)
            {
               System.out.println(--end);
            }
            else {
                System.out.println(start++);
            }
            return CountUpAndDown(end, first, start);
        }
    

提交回复
热议问题