As the title stated, I am trying to find the maximum and minimum values, the amount of evens and odds numbers, and the average of all inputted numbers.
The problem. As I run my code, my odds and evens counter seem to read their opposite, odd would read an even input and even would read an odd input. As for my average, I have no clue what is wrong with it, all I know is that it would only find the average of a proper fraction.
Also, as I input quit, I get for my largest number 214XXXXXXXX
Example of my output will be pasted at the end.
System.out.println("Enter a sequence of integers. Any non-integer to quit");
Scanner scan = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = 0;
int count = 0;
int sum = 0;
int oddsCounter = 0;
int evensCounter = 0;
int getInt;
double average = 0;
while (scan.hasNextInt()) {
getInt = scan.nextInt();
if (getInt % 2 == 0) {
evensCounter++;
System.out.println("even: " + evensCounter);
} else {
oddsCounter++;
System.out.println("odd: " + oddsCounter);
}
if (getInt < min) {
min = getInt;
} else if (getInt > max) {
max = getInt;
}
sum += getInt;
System.out.println("sum " + sum);
count++;
System.out.println("count " + count);
average = (double) sum / (count);
System.out.println("average " + average);
}
System.out.println("smallest: " + min);
System.out.println("largest: " + max);
System.out.println("even: " + oddsCounter);
System.out.println("odd: " + evensCounter);
System.out.println("average: " + average);
Result:
Enter a sequence of integers. Any non-integer to quit
9 //input 1
odd: 1
sum 9
count 1
average 9.0
3 //input 2
odd: 2
sum 12
count 2
average 6.0
7 //input 3
odd: 3
sum 19
count 3
average 6.333333333333333
1 //input 4
odd: 4
sum 20
count 4
average 5.0
q //input 5: QUIT
smallest: 1
largest: 7 //This should be 9
even: 0
odd: 4
average: 5.0
Result 2:
Enter a sequence of integers. Any non-integer to quit
q //quit
smallest: 2147483647 //This should be 0
largest: 0
even: 0
odd: 0
average: 0.0
Any help would be appreciated. Thank you!