Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Intro to Java help?) Write a static method named evenNumbers that accepts a Sca

ID: 3724346 • Letter: #

Question

(Intro to Java help?) 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

import java.util.Scanner;
class Main {
public static void main(String[] args) {
// call function.
evenNumbers();
}
public static void evenNumbers(){
// initial counts
int count = 0,evencount=0;
Scanner sc = new Scanner(System.in);
// loop till 3 evennumbers are found
while(evencount<3){
// get a number from user
System.out.print("Type a number:");
int n = sc.nextInt();
// check even and increment evencount
if(n%2==0){
evencount++;
}
// otherwise set evencount to zero
else{
evencount = 0;
}
//increment total count
count++;
}
// print output
System.out.println("Three even numbers found after " + count + " total entries");
}
}
/*
sample output
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
*/