我正在通過一個基本的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
做任何事情在你跳出來,我可能是失蹤?我很想知道這個問題,並且一直在辦公室裏對算法進行可視化處理,結果導致數學無法解決。
爲什麼你遞增ctr兩次:ctr = ctr + 2; – 2015-03-19 00:46:25