简体   繁体   English

递归方法,将计算至少n个整数的数组中前n个整数的和。 以第n个整数开头

[英]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. 到目前为止,这是我的代码,但是当我运行它并为n输入一个值时,程序以“数字和为:”结尾,仅此而已。 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). 这只是一个函数调用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 正如Siren P.提到的那样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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