【问题标题】:what is wrong with my method for avg? [duplicate]我的平均方法有什么问题? [复制]
【发布时间】:2013-09-15 09:02:36
【问题描述】:

是的,我知道这里有很多方法。这是任务的一部分。在这段代码中,除了输入等于 sum100 而不是 sum

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);
    }

}

}

【问题讨论】:

  • 你为什么对基本算术运算符使用不同的方法?
  • 是的。通常,average 方法会采用一个数组或 ArrayList 的数字,然后从中计算平均值。
  • 这是作业的要求。这周我们学习了方法,你能告诉我吗? :)

标签: java methods


【解决方案1】:

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

【讨论】:

  • 太棒了 :( 我希望有一个简单的解决方案。看起来如何...虽然?
  • 这已经在您的众多转发中得到了回答。如果您不阅读答案,为什么要发布问题?
  • 好吧,我显然需要更多指导。我是在老师的 0 输入下自己学习的。感谢您的帮助。我真的很想“得到”这个。我阅读了所有回复并尝试使用它们。令人高兴的是,我刚刚想通了。如果 sum>100 和 if sum
【解决方案2】:

问题在于您退出 @chrylis 提到的 while 循环的方式。因此,如果总和为&lt;= 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(所以平均值将是37/4 = 9.25),但是在显示结果时将计数器减1,因此得到的计数为3

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

do-while 循环是这样的:

do{

//here goes your code

}while (input != 0);

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 对,但为什么它对 sum>100 有效?这两种情况都会出现问题吗? sum>100 和 sum
    • 不,不一样,实际上您在 display 方法中意识到了这一点,因为您在一种情况下修改了计数器,但在另一种情况下没有。尝试按照@chrylis 的建议重新检查您的 while 循环的作用
    猜你喜欢
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多