用Java输入的平均用户数

时间:2014-11-20 17:04:42

标签: java

我试图获得用户输入数字的平均值。每当我输入数字时,输出总是 -

总和是6 多少个数字:4 平均值:2.0

需要while循环,遗憾的是不允许循环。我对Java很新,所以我的格式很草率。在我的代码中我的问题在哪里?

import java.util.Scanner;

public class LoopsEndingRemembering {

    public static void main(String[] args) {
        // program in this project exercises 36.1-36.5
        // actually this is just one program that is split in many parts

        Scanner reader = new Scanner(System.in);
        int count = 0;
        int sum = 0;
        int endingVariable = -1;
        int input = 0;
        double average;

        while (input >= endingVariable) {
            System.out.println("Type numbers: ");
            input = Integer.parseInt(reader.nextLine());
            sum += count;
            average = (double)sum / count;
            count++;

            if (input == endingVariable) {
                System.out.println("Thank you and see you later!");
                System.out.println("The sum is " + sum);
                System.out.println("How many numbers: " + count);
                System.out.println("Average: " + average);
                break;
            } 
        }
    }
}

4 个答案:

答案 0 :(得分:3)

这里有几个错误。

导致错误的主要原因是sum += count。您可以添加显示用户数量的数字,而不是添加用户输入的内容。因此它总是会添加0 + 1 + 2 + 3等。

另一个问题是,一旦您更改了上述内容,就必须检查<{1}} 之前是否将输入添加到总和中。所以你必须首先写input == endingVariable,然后计算它内部的平均值。否则,它会将您的if视为要计算的数字序列的一部分。

如果您发现输入与-1不同,则只需将输入添加到总和并增加计数。

所以:

  • 将输入而不是计数添加到总和中。
  • 仅在到达最终项目时计算平均值。
  • 仅在输入不是endingVariable时才将输入添加到总和中并递增计数器。

答案 1 :(得分:0)

试试这个...

import java.util.Scanner;

public class LoopsEndingRemembering {

public static void main(String[] args) {
    // program in this project exercises 36.1-36.5
    // actually this is just one program that is split in many parts

    Scanner reader = new Scanner(System.in);

    int count = 0;
    int sum = 0;
    int endingVariable = -1;
    int input = 0;
    double average;



    while (input >= endingVariable) {


        System.out.println("Type numbers: ");
        input = Integer.parseInt(reader.nextLine());
        sum += input;   // you were doing mistake here.
        average = (double)sum / count;
        count++;

        if (input == endingVariable) {
            System.out.println("Thank you and see you later!");
            System.out.println("The sum is " + sum);
            System.out.println("How many numbers: " + count);
            System.out.println("Average: " + average);
            break;

        } 

    }

}
}

答案 2 :(得分:0)

您的代码存在两个大问题。首先,这一行:

sum += count;

这可能只是一个错字,但显然应该是:

sum += input;

其次,一旦你解决了这个问题,你就会注意到你的总和中包含了-1。你可能想要这样的东西:

    System.out.println("Type numbers: ");
    input = Integer.parseInt(reader.nextLine());

    if (input == endingVariable) {
        average = (double) sum / count;

        System.out.println("Thank you and see you later!");
        System.out.println("The sum is " + sum);
        System.out.println("How many numbers: " + count);
        System.out.println("Average: " + average);
        break;
    } else {
        sum += input;
        count++;
    }

答案 3 :(得分:0)

感谢您详细解释。它现在工作正常,我所要做的就是你所说的是改变输入,并移动总和+ =输入并计算其他项目中的最终项目。我正在努力弄清楚为什么要包括我的-1

相关问题