-3

是的,我知道这里有很多方法。这是任务的一部分。在这段代码中,除了输入等于 sum<=100 的数字外,一切都按预期工作,“平均”输出是错误的。例如:如果我输入 8,10,19 和零退出输出是 count 3 sum 37 average 9.25.... 平均值应该是 12.3333。现在,如果我输入 8、10、99,输出是计数 3 和 117 和平均 39,这是正确的。为什么它适用于 sum>100 而不是 sum<=100 ???我不明白。我错过了什么?

public static void main(String[] args) {
    //Use Main Method for gathering input
    float input = 1;
    // Declare variable for sum
    float theSum = 0;
    // Declare variable for average
    float average = 0;
    // Declare variable for counting the number of user inputs
    int counter = 0;
    /* Initialize the while loop using an input of 0 as a sentinel value
     * to exit the loop*/
    while (input != 0) {
        if (input!=0){
            counter++;
        }
        input = Float.parseFloat(
                JOptionPane.showInputDialog(
                null, "Please enter a number.  Enter 0 to quit: "));
        // Invoke sum method and pass input and summation to sum method


        theSum = (sum(input, theSum));
        if (theSum > 100)
        {
            JOptionPane.showMessageDialog(null, "The sum of your numbers "
                    + "are greater than 100!");
            break;
        }
    }
        // Invoke display method and pass summation, average, and counter variables to it
                average = (avg(theSum, counter));    
                display(theSum, average, counter);
    }


public static float sum(float num1, float sum) {
    //Add the user's input number to the sum variable
    sum += num1;
    //Return value of sum variable as new summation variable
    return sum;
}

public static float avg(float num1, float num2) {
    //Declare and initialize variable for average
    //Calculate average
    float average = num1 / num2;
    //Return value of average variable
    return average;
}

public static void display(float sum, float average, int counter) {

    /* I am subtracting 1 from variable counter so as not to include the sentinel value
     * of 0 that the user had to enter to exit the input loop in the overall count*/

    // Display the count, sum, and average to the user
    if (sum > 100) {
        JOptionPane.showMessageDialog(null, "Count = " + (counter) + ", Sum = " + sum + ", Average = " + average);
    }
    if (sum <= 100) {
        JOptionPane.showMessageDialog(null, "Count = " + (counter - 1) + ", Sum = " + sum + ", Average = " + average);
    }

}

}

4

3 回答 3

4

原因是您while以不同的方式退出循环,具体取决于总和。如果总和小于 100,即使您输入0要“退出”的数字,您仍然会多循环一次。老实说,整个循环需要彻底重构;do...while循环会更容易阅读和调试。

于 2013-09-15T00:42:02.347 回答
0

您的计数器比必要的大 1。除以(counter - 1)将解决它。

于 2013-09-15T00:41:04.560 回答
0

问题是因为您退出 @chrylis 提到的 while 循环的方式。因此,如果总和是<= 100计数器,则计数器大 1。但是当您打印它时,您会得到正确的结果,因为您在此处更新了计数器值:

if (sum <= 100) {
        JOptionPane.showMessageDialog(null, "Count = " + (counter - 1) + ", Sum = " + sum + ", Average = " + average);
    }

正如您在示例中看到的那样:“如果我输入 8,10,19 和零退出输出是count 3 sum 37 average 9.25

这是因为计数器值是4(所以 avg 将是37/4 = 9.25),但是在显示结果时,您将 counter 减 1,因此您得到的计数为3

do-while 循环将解决该问题,因为条件将在最后检查,因此循环将以相同的方式退出<=100和 '>100'。

循环将do-while是这样的:

do{

//here goes your code

}while (input != 0);
于 2013-09-15T00:50:22.733 回答