到目前为止,这是我的代码,但是当我运行它并为n输入一个值时,程序以“数字和为:”结尾,仅此而已。不管我输入什么值都不会改变,您能帮助我弄清楚我做错了什么吗?

import java.util.Scanner;
class addNum
{
    //A method for Adding
    public static int addNum (int arr[], int n)
    {
      int x = 0;
      if (n > arr.length)
      {
          return 0;
      }
      else if (n == 1)
      {
          return 1;
      }
      else
      {
         x = arr[n-1] + addNum(arr, n);
         return n;
      }
     }

    public static void main(String args[])
    {
        int n = 0;
        int arr[] = {1,2,3,4,5,6,7};
        System.out.println("Input your number and press enter: ");
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        System.out.print("Sum of numbers is:");
        addNum(arr, n);
        System.out.println();
        }
    }

最佳答案

尝试这个:

public static int addNum (int arr[], int n)
        {
          int x = 0;
          if (n > arr.length)
          {
              return 0;
          }
          else if (n == 1)
          {
              //When n == 1, you want to return the first element of your array and not 1
              return arr[0];
          }
          else
          {
             //As you go deeper into recursion, you break your problem into a smaller problem.
             //Therefore, when calling addNum again, you pass n-1 and not n, as now you want to add remaining n-1 numbers
             x = arr[n-1] + addNum(arr, n-1);
             // you want to return your sum x and not n
             return x;
          }
         }

        public static void main(String args[])
        {
            int n = 0;
            int arr[] = {1,2,3,4,5,6,7};
            System.out.println("Input your number and press enter: ");
            Scanner in = new Scanner(System.in);
            n = in.nextInt();
            System.out.print("Sum of numbers is:");
            //Your addNum method returns an int, so you want to save it in a variable and then print it
            int x = addNum(arr, n);
            System.out.println(x);
            }

10-08 09:23