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

Write a complete Java program that will determine and display the average of a l

ID: 3625221 • Letter: W

Question

Write a complete Java program that will determine and display the average of a list of positive integers. The integers will be provided as input by the user, and the end of the list will be indicated by the user entering a negative number. The program should display the average as a double. If the first number entered is a negative number, give an appropriate error message.

Explanation / Answer

import java.util.Scanner; public class ListAverager { public static void main(String[] args) { Scanner in = new Scanner(System.in); int input, count = 0; double total = 0; System.out.println("Enter the numbers: "); input = in.nextInt(); if(input < 0) { System.out.println("Error: No elements in the list!"); } else { while(input >= 0) { total += input; count++; input = in.nextInt(); } System.out.println("The average of the numbers is " + total / count); } } }
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