Skip to main content
added 386 characters in body
Source Link
TorbenPutkonen
  • 9.4k
  • 21
  • 29

A few comments regarding style.

    int size = 0;

In the Java standard libraries the name "size" refers to number of elements in a collection. The use here is misleading because you're not tracking the size of something. you're counting the number of values that has been entered, so a better name would be count.

    double avg = 0;

The average of the input values is not needed during input processing so defining and updating it in in the loop is unnecessary. It is something that you can generate at the output phase from sum and count. You don't need the avg variable at all. If you would need it, just spell average. Unnecessary abbreviations just make code less readable.

    avg = ((avg*size)+n)/(size+1);

Average is the sum of entries divided by number of entries. This is a much too complicated way to calculate the average and increases inaccuracy (floating point numbers are not precise so every unnecessary mathematical opration reduces accuracy).

    double max = 0;
    double min = 0;
    double sum = 0;

By using primitive types and initializing them to zero you handily avoid the need to handle the special case where the user does not enter anything. But it doesn't make the program correct. If the user does not enter values, the minimum and maximum values were not zero, nor was the average. So you produce incorrect results. You have to use Double types initialized to null and make a check for count being zero when producing output.

This looks like class room code so I suppose the requirement to use zero as an exit condition came from your teacher. In general it is not a good idea to use a value that could be interpreted as valid input as an exit condition. Right now you can input positive and negative values but not a zero and that is a bit weird. An empty line would have been a better choice in my opinion.

A few comments regarding style.

    int size = 0;

In the Java standard libraries the name "size" refers to number of elements in a collection. The use here is misleading because you're not tracking the size of something. you're counting the number of values that has been entered, so a better name would be count.

    double avg = 0;

The average of the input values is not needed during input processing so defining and updating it in in the loop is unnecessary. It is something that you can generate at the output phase from sum and count. You don't need the avg variable at all. If you would need it, just spell average. Unnecessary abbreviations just make code less readable.

    double max = 0;
    double min = 0;
    double sum = 0;

By using primitive types and initializing them to zero you handily avoid the need to handle the special case where the user does not enter anything. But it doesn't make the program correct. If the user does not enter values, the minimum and maximum values were not zero, nor was the average. So you produce incorrect results. You have to use Double types initialized to null and make a check for count being zero when producing output.

A few comments regarding style.

    int size = 0;

In the Java standard libraries the name "size" refers to number of elements in a collection. The use here is misleading because you're not tracking the size of something. you're counting the number of values that has been entered, so a better name would be count.

    double avg = 0;

The average of the input values is not needed during input processing so defining and updating it in in the loop is unnecessary. It is something that you can generate at the output phase from sum and count. You don't need the avg variable at all. If you would need it, just spell average. Unnecessary abbreviations just make code less readable.

    avg = ((avg*size)+n)/(size+1);

Average is the sum of entries divided by number of entries. This is a much too complicated way to calculate the average and increases inaccuracy (floating point numbers are not precise so every unnecessary mathematical opration reduces accuracy).

    double max = 0;
    double min = 0;
    double sum = 0;

By using primitive types and initializing them to zero you handily avoid the need to handle the special case where the user does not enter anything. But it doesn't make the program correct. If the user does not enter values, the minimum and maximum values were not zero, nor was the average. So you produce incorrect results. You have to use Double types initialized to null and make a check for count being zero when producing output.

This looks like class room code so I suppose the requirement to use zero as an exit condition came from your teacher. In general it is not a good idea to use a value that could be interpreted as valid input as an exit condition. Right now you can input positive and negative values but not a zero and that is a bit weird. An empty line would have been a better choice in my opinion.

Source Link
TorbenPutkonen
  • 9.4k
  • 21
  • 29

A few comments regarding style.

    int size = 0;

In the Java standard libraries the name "size" refers to number of elements in a collection. The use here is misleading because you're not tracking the size of something. you're counting the number of values that has been entered, so a better name would be count.

    double avg = 0;

The average of the input values is not needed during input processing so defining and updating it in in the loop is unnecessary. It is something that you can generate at the output phase from sum and count. You don't need the avg variable at all. If you would need it, just spell average. Unnecessary abbreviations just make code less readable.

    double max = 0;
    double min = 0;
    double sum = 0;

By using primitive types and initializing them to zero you handily avoid the need to handle the special case where the user does not enter anything. But it doesn't make the program correct. If the user does not enter values, the minimum and maximum values were not zero, nor was the average. So you produce incorrect results. You have to use Double types initialized to null and make a check for count being zero when producing output.