We Are Going To Discuss About Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.). So lets Start this Java Article.
Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.)
- Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.)
When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with
in.nextInt()
because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error. - Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.)
When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with
in.nextInt()
because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error.
Solution 1
When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with in.nextInt()
because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error.
while ((input = in.nextInt()) != 0) {
if (counter == 0)
small = large = input;
if (input != 0)
sum = input + sum;
counter++;
if (input > large)
large = input;
if (input < small)
small = input;
if (input % 2 == 0)
even = even + 1;
else
odd = odd + 1;
}
Original Author devilfart Of This Content
Solution 2
You are inputting extra numbers in the loop body for large
and small
. Use ++
instead of += 1
, I would prefer Integer.min
and Integer.max
; initialize small
and large
to something very large and very small respectively. Something like,
double even = 0, odd = 0, sum = 0;
int counter = 0, input = 0, large = Integer.MIN_VALUE, small = Integer.MAX_VALUE;
System.out.print("Enter a series of values (0 to quit): ");
Scanner in = new Scanner(System.in);
while ((input = in.nextInt()) != 0) {
small = Integer.min(small, input);
large = Integer.max(large, input);
sum += input;
counter++;
if (input % 2 == 0) {
even++;
} else {
odd++;
}
}
if (counter > 0) {
double average = sum / counter;
System.out.println("The smallest integer is: " + small);
System.out.println("The largest integer is: " + large);
System.out.println("Total number of integers entered is " + counter);
System.out.println("Total even numbers entered is " + even);
System.out.println("Total odd numbers entered is " + odd);
System.out.println("The average value is: " + average);
} else {
System.out.println("No data was entered.");
}
Original Author Elliott Frisch Of This Content
Solution 3
WAP to input a number and check ut is even ir odd until user user input is zero using infinite for loop.
Using format:
for(;;)
{
}
Original Author Utkarsh Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.