Question
In Java Write an application called EvenOrOddCounter that prompts the user for integer numbers and counts how many of the numbers entered were even and
In Java
Write an application called EvenOrOddCounter that prompts the user for integer numbers and counts how many of the numbers entered were even and how many were odd until a 0 is entered. There is additional information about while loops with sentinels at the end of this document.
Your program should:
use a while loop
prompt the user to enter a integer values until a 0 is entered
your codes prompt must resemble the examples below
determine if the value is even or odd. Hint: when even numbers are divided by 2 the remainder is 0
count the number of even values entered and the number of odd values entered
display results as shown in the example below
if the user enters a 0 for the first number, indicate that no values were entered
Listed below are two example outputs. Of course, your output can be different depending on the values entered.
Example 1:
Even or Odd Counter
----------------------
Please enter a series of integer values. 0 to quit.
Enter value 1: 34
Enter value 2: -2
Enter value 3: 3
Enter value 4: 0
The Results
-----------
Total values entered: 3
Even values entered : 2
Odd values entered : 1
Example 2:
Even or Odd Counter
----------------------
Please enter a series of integer values. 0 to quit.
Enter value 1: 0
The Results
-----------
No Values were entered.
More information about while loops and sentinels
Sometimes we dont know in advance how many values will be entered by the user. In a situation such as this, we ask the user to enter a sentinel value to indicate that no more values will be entered. When a user enters the sentinel, the loop terminates.
This assignment is an example of using a sentinel to signal that there are no more values to be entered. In this assignment 0 is the sentinel value.
The following is an outline of the general structure of using a while loop with a sentinel.
initialize variables
read the first value from the user //priming read
while (value != sentinel)
{
determine if the value is even or odd
increment the appropriate counter
do any other work
read the next value from the user //update read
}
report out results
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started