Whats wrong with my code? Why isn't my minimum working? Thanks.

• August 28th, 2014, 05:32 PM
judyfinny
Whats wrong with my code? Why isn't my minimum working? Thanks.
public class doubleNumber
{
public static void main (String[] args) {
double sum = 0;
int count = 0;
double userEnterNumber = 0;
double minimum = 0;
double maximum = 0;
double average = 0;
System.out.println(" Input numbers, type a non-numerical digit to exit: ");
Scanner in = new Scanner(System.in);

while ( in.hasNextDouble())
{
userEnterNumber = in.nextDouble();

if (userEnterNumber>0) {

sum = sum + userEnterNumber;
count ++;
average = sum/count;
if (userEnterNumber >maximum){
maximum = userEnterNumber;
}
else if (userEnterNumber <minimum){
minimum = userEnterNumber;
}
}

}
System.out.println(" Sum: "+sum);
System.out.println(" Count: "+count);
System.out.println(" Average: "+average);
System.out.println(" Maximum: "+maximum);
System.out.println(" Minimun: "+minimum);
}
}
• August 28th, 2014, 05:44 PM
GregBrannon
Re: Whats wrong with my code? Why isn't my minimum working? Thanks.
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

• August 28th, 2014, 05:52 PM
Norm
Re: Whats wrong with my code? Why isn't my minimum working? Thanks.
A trick when searching for max and min values is to initialize the variables with extreme values. Make min the largest possible value and max the smallest.
• August 28th, 2014, 07:06 PM
Junky
Re: Whats wrong with my code? Why isn't my minimum working? Thanks.
I thought all digits were numerical!

Code :

`else if (userEnterNumber <minimum){`
The way your code is written this will never be true. As per Norm's hint.

Also, if I gave you five numbers how many times would you calculate the average? How many times does your code calculate it?