简体   繁体   中英

Recursive method that will compute the sum of the first n integers in an array of at least n integers. Begin with nth integer

This is my code so far but when I run it and enter a value for n the program ends with "Sum of numbers is: " and that's all. Doesn't change no matter what value I enter, can you help me figure out what I'm doing wrong?

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();
        }
    } 

try changing it to

    System.out.println(addNum(arr, n));

so something is actually returned and printed

and there is a bug

x = arr[n-1] + addNum(arr, n);
return x;  // not n
addNum(arr, n);

This is just a function call addNum(param1, param2).

It will only be returning value, not printing out value. Thus, you have to print that value out in order to be able to see it.

System.out.println(addNum(arr, n));

As mentioned by Siren P. will work

Try this:

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);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM