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

Write a program in java that reads an unspecified number of integers, determines

ID: 641702 • Letter: W

Question

Write a program in java that reads an unspecified number of integers, determines how many positive and negative values have been entered, and computes the sum and average of the input values (not counting zeros). The reading of input ends when the user enters 0 (zero). Display the number of positive and negative inputs, the sum and the average. The average should be computed as a floating-point number. Design the program such that it asks the user if they want to continue with new inputs after each set of entries, ending the program only when they do not respond to the question with "yes".

Here is a sample run:

Explanation / Answer

Below is the self-explanatory code as per your request..

import java.util.Scanner;

public class Program {
   /*
   * Input a list of integers (end with 0): 1 2 -1 3 0
   *
   * # of positive inputs: 3 # of negative inputs: 1 The total: 5.0 The
   * average: 1.25
   *
   * Would you like to continue with new inputs? yes Input a list of integers
   * (end with 0): 0
   *
   * No numbers were entered except 0
   *
   * Would you like to continue with new inputs? no
   */
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       while (true) {

           System.out.print("Input a list of integers (end with 0): ");
           int numberOfPositiveInputs = 0;
           int numberOfNegativeInputs = 0;
           int count = 0;
           int nextInt = 0;
           float total = 0;

           while (true) {
               nextInt = scanner.nextInt();
               if (nextInt == 0) {
                   if (count == 0) {
                       System.out.println("No numbers were entered except 0");
                   } else {
                       System.out.println("# of positive inputs: " + numberOfPositiveInputs);
                       System.out.println("# of negative inputs: " + numberOfNegativeInputs);
                       System.out.println("The total: " + total);
                       System.out.println("The average: " + (total / count));
                   }
                   break;
               } else {
                   if (nextInt > 0) {
                       numberOfPositiveInputs++;
                   } else {
                       numberOfNegativeInputs++;
                   }
                   total += nextInt;
                   count++;
               }
           }

           System.out.print("Would you like to continue with new inputs?");
           String nextInput = scanner.next();
           if ("yes".equalsIgnoreCase(nextInput)) {
               continue;
           } else if ("no".equalsIgnoreCase(nextInput)) {
               break;
           }
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote