|
Re: Need min w/o it including the 0 I use to quit
|
Posted: Apr 4, 2002 9:07 PM
|
|
min is decalred as a global class variable and class variables are always implicitly initialized when not explicitly initialize - null, 0, '', and false. (local variables however are uninitialized)
min therefore starts out as 0,which is lower than any valid input.
try initializing min and max like such...
min=Integer.MAX_VALUE; max=Integer.MIN_VALUE;
or better, read the first number outside of the loop, set min and max to the first number, (the only time you will need to compare both min and max to number. ) this may tak an extra line of code ( great for Bradbury style coding "why say it eloquently in a single line when two would do? - he got paid by the word. " ), but if you type really fast, (or use a faster input stream ) you could save a gonculation per loop, and keep your cpu nice and cool.
number = Integer.parseInt (stdin.readLine());
min=number; max=number;
while(number!=0)
{
number = Integer.parseInt (stdin.readLine());
if(number=0) break;
if(min > number)min=number;
else if(max < number)max=number;
/*
adding else should cut the if(max<number) checks by an average of 50%
Now, kick back for a few nanoseconds.
*/
// etc
}
Now min will be sure to change at the first checkpoint, min will never be zero.
|
|