Java逻辑错误与基础数学
问题描述:
我正在通过一个基本的java课程,并且有一个非常困难的时间思考编程式的盟友,所以我忍受着这里。我应该编写一个程序,在用户定义的范围内总结所有的奇数 - 简单的权利?嗯,我认为我的代码已经算出来了,但数学总是出错。代替1到14等于19(1 + 3 + 5 ...)的范围,程序返回46.它只有3,所以我觉得我正在接近正确的代码。Java逻辑错误与基础数学
下面是电流输出样本:
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的值已经由两个
增加,以致
sum = 0;
for (ctr = 1; ctr <= topEndNumber; ctr = ctr + 2) {
sum += ctr;
}
会给你所需要的答案。
+0
非常感谢,一旦我找到了它与我现有的代码相适合的方式,我就完全工作了。 – user132791 2015-03-19 21:40:08
为什么你递增ctr两次:ctr = ctr + 2; – 2015-03-19 00:46:25