I been working on this all night but couldn't make anything out it. I want my code to sum all the numbers the user enter, count how many times the user enters the number. then calculate the average. and then find the max and min, easy right. well yeah if i was to be allowed to use arrays but this is for review and I hate while loops.
here's my code.
double integer = 1;
//double num = 0;
double sum = 0.0;
double average = 0.0;
Scanner input = new Scanner(System.in);
int count = 0;
// double char1=0;
double min = integer;
double max = integer;
// char letter = 'q';
while (integer != 0) {
System.out.println("Please enter an integer: ");
integer = input.nextInt();
sum += integer;
count++;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
if (integer > max)
max = integer;
else if (integer < min)
min = integer;
}
Here's the output:
Please enter an integer: 3 The sum of your numbers is: 3.0 The number of values entered is: 1
Please enter an integer: 2 The sum of your numbers is: 5.0 The number of values entered is: 2
Please enter an integer: 1 The sum of your numbers is: 6.0 The number of values entered is: 3
Please enter an integer: 0 The sum of your numbers is: 6.0 The number of values entered is: 4
The average of your sum is: 1.5 The max integer is: 3.0 The min integer is: 0.0
when the count increases by 1 my average comes out wrong. but why is 0 been counted as part of count and why my min always output 0 and not what the user enters. any and all help is much appreciated.
p.s. i have tried numerous ways but it doesnt work. if i try to change my count to start at -1 it solves my problem at hand with average but the count increases anyways so i know its incorrect. also the min problem stays there.
thanks guys