gpt4 book ai didi

Java 基本数学逻辑错误

转载 作者:行者123 更新时间:2023-12-02 04:52:05 25 4
gpt4 key购买 nike

我正在一瘸一拐地学习一个基本的 java 类,并且很难以编程方式思考,所以请耐心等待。我应该编写一个程序,将用户定义范围内的所有奇数相加 - 很简单吧?好吧,我以为我已经弄清楚了代码可以做到这一点,但数学总是错误的。程序返回 46,而不是 1 到 14 的范围等于 19 (1 + 3 + 5 ...)。它只相差 3,所以这让我觉得我已经非常接近正确的代码了。

这是当前的示例输出:

The value input is 14
DEBUG: The current value of variable sum is: 4
DEBUG: The current value of variable ctr is: 3
DEBUG: The current value of variable sum is: 10
DEBUG: The current value of variable ctr is: 7
DEBUG: The current value of variable sum is: 22
DEBUG: The current value of variable ctr is: 11
DEBUG: The current value of variable sum is: 46
DEBUG: The current value of variable ctr is: 15
The sum of the odd numbers from 1 to 14 is 46

麻烦的方法如下:

    public static void calcSumPrint(int topEndNumber) {
//calc and print sum of the odd number from 1 to top-end number
//uses loop to add odd numbers
//display results: range (eg: 1 to 13), sum of odd numbers
for (ctr = 1; ctr <= topEndNumber; ctr = ctr + 2) {
nextOddNumber = sum + 2;
sum = sum + nextOddNumber;
ctr = ctr + 2;
if (debug) {
System.out.println("DEBUG: The current value of variable sum is: " + sum);
System.out.println("DEBUG: The current value of variable ctr is: " + ctr);
}
}

System.out.println("The sum of the odd numbers from 1 to " + topEndNumber + " is " + sum);

}//end of calcSumPrint

这是程序:

import java.util.Scanner;

public class sumOdds {
static int topEndNumber = 0;
static int ctr = 0;
static int intermediateSum = 0;
static int sum = 1;
static boolean debug = true;
static int nextOddNumber = 0;


public static void main(String[] args) {
getLimitNumber();
System.out.println("The value input is " + topEndNumber);
calcSumPrint(topEndNumber);

}//end of main

public static int getLimitNumber() {
//lets uer input top-end number to be used in program [X]
//catches exception if non-integer value is used [X]
//verifies that the input number has a value of at least 1 [ ]
//returns verified int to method caller [ ]
Scanner input = new Scanner(System.in);
boolean done = false;
while (done != true) {
try {
System.out.println("Enter a positive whole top-end number to sum odds of:");
topEndNumber = input.nextInt();
if (topEndNumber <= 0){
throw new NumberFormatException();
}
done = true;
}//end of try
catch (Exception message) {
//put exception in here
input.nextLine();
System.out.println("Bad input, retry");
}//end of catch
}//end of while
input.close();


//to shut up eclipse
return topEndNumber;


}//end of getLimitNumber

public static void calcSumPrint(int topEndNumber) {
//calc and print sum of the odd number from 1 to top-end number
//uses loop to add odd numbers
//display results: range (eg: 1 to 13), sum of odd numbers
for (ctr = 1; ctr <= topEndNumber; ctr = ctr + 2) {
nextOddNumber = sum + 2;
sum = sum + nextOddNumber;
ctr = ctr + 2;
if (debug) {
System.out.println("DEBUG: The current value of variable sum is: " + sum);
System.out.println("DEBUG: The current value of variable ctr is: " + ctr);
}
}

System.out.println("The sum of the odd numbers from 1 to " + topEndNumber + " is " + sum);

}//end of calcSumPrint

public static int doAgain() {
//ask and verify the user wants to re-run program, return int

//to shut up eclipse
return 20000;
}//end of doAgain

}//end of class

你有没有注意到我可能错过的任何事情?我很想弄清楚这个问题,并且一整天都在办公室里断断续续地想象这个算法,这让我发疯,数学不起作用。

最佳答案

for 循环中,ctr 的值已经增加了 2

所以

sum = 0;
for (ctr = 1; ctr <= topEndNumber; ctr = ctr + 2) {
sum += ctr;
}

将为您提供所需的答案。

关于Java 基本数学逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29135317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com