基本数学的Java逻辑错误

时间:2015-03-19 00:44:16

标签: java

我一瘸一拐地通过一个基本的java课程,并且非常难以想象程序化,因此请耐心等待。我应该编写一个程序,将用户定义范围内的所有奇数求和 - 简单对吧?好吧,我以为我已经找到了代码,但数学总是出错。程序返回46而不是等于19(1 + 3 + 5 ...)的1到14的范围。它只有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

有什么事情突然发现我可能会失踪吗?我很乐意把这一点弄清楚,并且在办公室全天都可以看到算法,这让我疯狂,数学无法解决。

1 个答案:

答案 0 :(得分:2)

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

所以

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

会给你所需的答案。