【问题标题】:Can you help me fix the average portion of my code? [duplicate]你能帮我修复我的代码的平均部分吗? [复制]
【发布时间】:2013-09-14 03:14:06
【问题描述】:

当我运行此代码并输入总和>100 的数字时,输出对于计数和总和来说是正确的,但平均值是错误的。例如;输入 8,10,99...计数为 3,总和为 117,应返回平均值 39...返回的实际输出为计数 3,总和为 117,平均值为 58.5。我开始意识到这是因为平均值是使用 2 而不是 3 的计数来完成的(或者总是比它应该使用不同的值小一)。为什么是这样?它非常适合输入总和

 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) {
        // Use JOptionPane method to accept input from user
        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));
        // Invoke avg method and pass summation and counter to avg
        average = (avg(theSum, counter));
        // Increment the counter variable
        counter++;

        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
        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, int num2) {
    //Declare and initialize variable for average
    float average = 0;
    //Calculate average
    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);
    }

}

}

【问题讨论】:

    标签: java average


    【解决方案1】:

    取平均值后递增计数器,这就是为什么您看到的平均值是基于 2 个数字而不是您预期的 3 个。

        average = (avg(theSum, counter));
        // Increment the counter variable
        counter++;
    

    交换这两个并增加计数器之前你取平均值。

        counter++;
        // Increment the counter variable
        average = (avg(theSum, counter));
    

    编辑:

    以下是您应该更改的内容:

    首先,仅当输入不 = 0 时才更新计数器

        if(input!=0)
        {
        counter++;
        }
    

    其次,将平均代码移出循环并在显示之前将其放在最后,不需要一次又一次地计算平均值。

        average = (avg(theSum, counter));
        display(theSum, average, counter);
    

    第三,从display方法中删除counter-1并打印counter

    public static void display(float sum, float average, int counter) {
     JOptionPane.showMessageDialog(null, "Count = " + (counter) + ", Sum = " + sum + ", Average = " + average);
     }
    

    之后,它适用于您所期望的两种情况

    【讨论】:

    • 谢谢!!!!我已经疯了!!!工作就像一个魅力,你是最好的:)
    • BUUUUTTTT,现在 sum
    • @Chris:太好了,你有答案了。 meta.stackoverflow.com/help/someone-answers。它可以帮助您在将来获得更好的帮助..
    • 克里斯,您需要从您的消息对话框中删除:(counter - 1)。不管你的总和是多少,都做同样的事情。
    • counter-1 是为了说明“0 退出”我不希望它包含在计数中......无论哪种方式。我仍然得到相同的错误输出:(当我输入 8,10,19 时,总和为 37,返回的平均值为 9.25。它应该是 12.33333。奇怪的是,无论 -1 是什么,输出都是相同的。
    猜你喜欢
    • 2023-02-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多