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

oo T-Mobile LTE 7:23 PM 82% F.les labs7 8 9 10-mcc.docx Lab 8 Ask the user to en

ID: 3888203 • Letter: O

Question

oo T-Mobile LTE 7:23 PM 82% F.les labs7 8 9 10-mcc.docx Lab 8 Ask the user to enter an integer (positive or negative does not matter). Repeat the integer given by the user and tell them whether it is even or odd. After the user enters the first integer, and after each subsequent integer entered, ask the user if they wish to enter another integer. If they answer 'Y' or 'y, then repeat, if they answer .N. , or any other character then tell them how many odd numbers they entered and how many even numbers they entered. Note that the user should always enter at least one integer- only ask them if they have another after the first entry Think about the three types of loops we have learned (while, do-while and for) and choose the most appropriate type for this problem. You should test your program and make sure it works before asking me to check it off! The examples below are test cases. Your code should work properly with all test cases listed, as well as with other inputs. Please test your code with all test cases included with each lab, and have these test cases already run and on screen when you call me over to check you off Example Output: Test Case 1 Enter an integer: 7 is odd Do you have another integer to enter (Y/N)?: Y Enter an integer: -5> -5 is odd Do you have another integer to enter (YIN)?: y Enter an integer: 16 is even. Do you have another integer to enter (Y/N)?: N Odd numbers: 2 Even numbers: 1 Courses Calendar Messages

Explanation / Answer

package com.vassarlabs.test.practise;

import java.util.Scanner;

public class EvenOdd {
  
   public static void main(String args[]) {
       Scanner scanner = new Scanner(System.in);
       int evenCount = 0, oddCount = 0;
      
       while (true) {
           System.out.print("Enter an integer : ");
           int inputValue = Integer.parseInt(scanner.next().trim());
          
           if (inputValue%2 == 0) { /* if divisible by 2 (Even) */
               System.out.println(inputValue + " is even");
               evenCount++;
           }
           else {
               System.out.println(inputValue + " is odd");
               oddCount++;
           }
          
           System.out.print("Do you have another integer to enter(Y/N)? : ");
           String choice = scanner.next().trim();
          
           if (choice.equalsIgnoreCase("Y")) {
               continue;
           }
           break;
       }

       System.out.println("Odd Numbers : " + oddCount);
       System.out.println("Even Numbers : " + evenCount);  
       scanner.close();
   }

}