|
Re: Average
|
Posted: Nov 26, 2003 5:25 AM
|
|
Just count the inputs. If your input Command is not in the method wich builds the sum, use a static variable wich you increment after the input, if the input was not 0. At the end, do something like sum / java.lang.Math.max (1, nInputs) I used the max method here, in case you don't want the last 0 to be used for the
The code without a static variable would look like this:
int counter = 0 double sum = 0; do { double val = input(); //the input text gets converted to double/float/int inside the input-Method. sum += val; if (val != 0) counter++; } while (val != 0); double average = sum / java.lang.Math.max (1, counter);
This REALLY isn't a Java problem, it's just basic programming.
|
|