Write a static method named evenNumbers that accepts a Scanner from the console.
ID: 3640461 • Letter: W
Question
Write a static method named evenNumbers that accepts a Scanner from the console.The method should repeatedly prompt the user to enter integers until 3 consecutive even integers are entered.
When 3 consecutive even numbers are seen, the method should output a message including the total number of entries.
You may assume that the user will enter a single integer after each prompt. Here is a sample dialog of user interaction with the program.
Looking for three even numbers in a row.
Type a number: 44
Type a number: 15
Type a number: 23
Type a number: 2
Type a number: 12
Type a number: 34
Three even numbers found after 6 total entries
Explanation / Answer
public static void evenNumbers(Scanner in)
{
int count = 0;
int n = 0;
int i =0;
while(true)
{
System.out.println(" Type a number: ");
n = in.nextInt();
i++;
if(n%2 == 0) count++;
else count = 0;
if(count == 3) break;
}
System.out.println(" Three even numbers found after " + i + " total entries");
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.