Java逻辑错误与基本数学
我正在蹒跚学完一个基本的java类,很难从编程的角度思考问题,所以请耐心听我说。我应该写一个程序,对用户定义范围内的所有奇数求和-简单,对吗?嗯,我以为我已经找到了实现它的代码,但是数学总是出错。程序返回46,而不是1到14的范围等于19(1+3+5…)。它只差3,所以我觉得我已经非常接近正确的代码了 以下是当前的示例输出:Java逻辑错误与基本数学,java,Java,我正在蹒跚学完一个基本的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 curr
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
公共静态无效calcSumPrint(int topEndNumber){
//计算并打印从1到顶端的奇数之和
//使用循环添加奇数
//显示结果:范围(例如:1到13)、奇数和
对于(ctr=1;ctr,在for
循环中,ctr的值已经增加了2
所以
sum=0;
对于(ctr=1;ctr为什么要将ctr增加两次:ctr=ctr+2;非常感谢,一旦我弄清楚它如何与我现有的代码相匹配,它就完全起作用了。
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
sum = 0;
for (ctr = 1; ctr <= topEndNumber; ctr = ctr + 2) {
sum += ctr;
}