【问题标题】:Method with return-statement doesn't return anything [duplicate]带有返回语句的方法不返回任何内容[重复]
【发布时间】:2020-04-22 01:14:23
【问题描述】:

我必须编写一个方法int sumBeyond(int k) 来找到最小的n,使得小于n 的自然数之和超过k。 但是,当我尝试测试该方法时,它不会返回任何值。

public static int sumBeyond(int k){
    int i=1;
    int sum=0;
    while(i<k){
        sum=sum+i;
        i=i+1;
    }
    return sum;
}

我尝试在 main 方法中以这种方式调用function

sumBeyond(100);

【问题讨论】:

  • 如果您只是忽略示例中的返回值,您怎么知道它不会返回任何内容。你试过int myReturn = sumBeyond(100); System.out.println(myReturn);之类的吗?
  • 它肯定会返回一个值,但如果你只调用sumBeyond(100);,你将看不到任何输出......让它System.out.println(sumBeyond(100));
  • 我会写 while(sum&lt;k) 但是,你的程序应该返回一些东西。
  • @ArnaudDenoyelle 它也应该返回 i(或者可能是 i+1 或 i-1,现在没有时间考虑)而不是 sum。

标签: java return


【解决方案1】:
int sum100 = sumBeyond(100);
System.out.println("Sum is " + sum100);

然后是小改进:

public static int sumBeyond(int k) {
    int i = 1;
    int sum = 0;
    while (i < k) {
        sum += i;
        ++i;
    }
    return sum;
}

public static int sumBeyond(int k) {
    int sum = 0;
    for (int i = 1; i < k; ++i) {
        sum += i;
    }
    return sum;
}

public static int sumBeyond(int k) {
    // return (k - 1) * (1 + k - 1) / 2;
    return (k - 1) * k / 2;
}

解决上述问题:

  • 求 n 使得总和达到 n-1 >= k',其中 k' 为 k - 1。
  • n-1 的总和是(n - 1) * n / 2 &gt;= k'

所以我们得到:

x² - x - 2k'
------------  >=  0
     2

= 0 的解决方案:

a = 1/2
b = -1/2
c = -2k'
            _________
    -b +/- V b² - 4ac
x = ------------------
          2a

 x = 1/2 +/- sqrt(1/4 + 4k'/4) =
   = 1/2 +/- 1/2 . sqrt(1 + 4k')

正x

 x = 1/2 + 1/2 . sqrt(4k' + 1)

public static int sumBeyond(int k) {
    double x = (Math.sqrt(4 * (k-1) + 1) + 1) / 2;
    return (int) Math.ceil(x);
}

解决方案应该给出数学作为注释。

【讨论】:

  • 我只想指出 OP 的另一个改进,可能是将其更改为 for 循环以提高可读性,因为这基本上是他对 i 所做的事情。
  • @Nexevis 是的,添加了它;可能是一种新的语言结构。
  • @Turing85 我只是在做,虽然那是更多的数学洞察力:一个智能解决方案,我们程序员并不总是...
  • 数学很优雅,但问题陈述是“找到最小的 n,使得小于 n 的自然数之和超过 k。”为什么要返回总和?但实际上,正如@ArnaudDenoyelle 评论的那样,我们需要像 while (sum
  • 不错。我也用二次公式勾勒出了一种方法。有一个我想测试的情况,因为根均匀地出来所以它是它的 Math.ceil(),我想我必须加 1(在那种情况下我有点担心浮点错误)。
【解决方案2】:

如果我的问题正确,您希望 找到最小的 n 使得小于 n 的自然数之和超过 k,因此,您不应该返回sum 本身,因为它不是n,而是需要计算才能找到最小的n

你可以这样做:

public static int sumBeyond(int k) {
    int n = 0;
    int sum = 0;

    for (int i = 0; i < k; i++) {
        // provide an intermediate sum (the one before this step) for logging purpose
        int intermediateSum = sum;
        // sum up
        sum += i;
        // set the return value to the current "natural number"
        n = i;
        // print some detailed debug log
        System.out.println("sum:\t" + sum +
                " (" + intermediateSum + " + " + i +  ")\t——>\tn = " + n);

        // exit the loop if the sum is greater than k
        if (sum >= k) {
            break;
        }
    }

    return n + 1;
}

像这样在main 中调用它

public static void main(String[] args) {
    int k = 100;
    System.out.println("The least n" +
            + "such that the sum of the natural numbers smaller than n exceeds "
            + k + " is " + sumBeyond(k));
}

将打印

sum:    0 (0 + 0)       ——> n = 0
sum:    1 (0 + 1)       ——> n = 1
sum:    3 (1 + 2)       ——> n = 2
sum:    6 (3 + 3)       ——> n = 3
sum:    10 (6 + 4)      ——> n = 4
sum:    15 (10 + 5)     ——> n = 5
sum:    21 (15 + 6)     ——> n = 6
sum:    28 (21 + 7)     ——> n = 7
sum:    36 (28 + 8)     ——> n = 8
sum:    45 (36 + 9)     ——> n = 9
sum:    55 (45 + 10)    ——> n = 10
sum:    66 (55 + 11)    ——> n = 11
sum:    78 (66 + 12)    ——> n = 12
sum:    91 (78 + 13)    ——> n = 13
sum:    105 (91 + 14)   ——> n = 14
The least n such that the sum of the natural numbers smaller than n exceeds 100 is 15

我真的希望我做对了,但仍然不确定......
哦,如果0 不是自然数,则从1 开始迭代。

【讨论】:

  • 所以不管0是否自然,“小于n的自然数之和超过100的最小n为14”是不正确的。小于(注意不是
  • @JeremyKahan 当然,你是对的......更正
【解决方案3】:

如上所述,问题不在于您的方法没有返回某些内容,而是您没有对返回的内容做任何事情。此外,如上所述,您专注于求总和,但这实际上并不是问题所问的。 我很同情你在这里使用 while 循环,因为它可能根本不会执行,而且你不知道它会运行多少次。所以我重写了它以检查正确的事情,并改编了 deHaar 的主要内容来练习它。这让我可以亲自检查答案,因为一些相等的情况以及对“数字小于”而不是“数字小于或等于”的需求是微妙的。 我的数学老师真的很喜欢 Joop Eggen 的二次公式方法;只是更难做到正确(事实上,如果我要这样做,我最终会测试它是否与我在这里所拥有的一致)。

public class ShadesOfLittleGauss {
    public static int sumBeyond(int k) {
        int i = 1; //have summed (trivially) all natural numbers less than 1 so far
        int sum = 0;
        while (sum <= k) { //needs to exceed, so <=
            sum = sum + i;
            i = i + 1;
        }
        return i;
    }
public static void main(String[] args) {
    for (int k = -1; k < 10; k++) {
        System.out.println("The least number " +
            "such that the sum of the natural numbers smaller than n exceeds " +
            k + " is " + sumBeyond(k));

    }
}
}

输出(您可以手动检查这些是否正确):

The least number such that the sum of the natural numbers smaller than n exceeds -1 is 1
    The least number such that the sum of the natural numbers smaller than n exceeds 0 is 2
    The least number such that the sum of the natural numbers smaller than n exceeds 1 is 3
    The least number such that the sum of the natural numbers smaller than n exceeds 2 is 3
    The least number such that the sum of the natural numbers smaller than n exceeds 3 is 4
    The least number such that the sum of the natural numbers smaller than n exceeds 4 is 4
    The least number such that the sum of the natural numbers smaller than n exceeds 5 is 4
    The least number such that the sum of the natural numbers smaller than n exceeds 6 is 5
    The least number such that the sum of the natural numbers smaller than n exceeds 7 is 5
    The least number such that the sum of the natural numbers smaller than n exceeds 8 is 5
    The least number such that the sum of the natural numbers smaller than n exceeds 9 is 5

更新:我确实解决了二次方并提出了以下与更简单的方法一致的方法。

public static int sumBeyond2(int k) {
if (k < 0) { //do not take squareroots of negatives
    return 1;
}
double x = -1.0 / 2 + Math.sqrt(1 + 8 * k) / 2; //from solving quadratic inequality n(n+1)/2.0 > k
if (Math.abs(Math.round(x) - x) < 0.0000001) { //special case, it is an integer, so ceil won't reliably add 1
    return 1 + 1 + (int) Math.round(x);
}
return 1 + (int) Math.ceil(x); //adding 1 because of wording, integers less than, ceil because needs to exceed k
}

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多