2 Statistics (30 points) Write a program called Average java that prompts a user
ID: 3742788 • Letter: 2
Question
2 Statistics (30 points) Write a program called Average java that prompts a user to enter a number, and allows the user to continue to enter numbers until she responds with a negative number. At that point, the program should print out how many numbers the user entered (not including the negative one), and the average of those numbers. Note: This program will use a Scanner object such as you learned about in CS I. This is the only occasion in this class that you are likely to use a Scanner object for user interaction. Yo be using command-line arguments or GUI widgets in every other program you write (or, sometimes, not interacting with a user at all) java Average Enter a series of numbers Enter a negative number to quit. 4.5 5 -1 You entered 5 numbers averaging 3.1Explanation / Answer
import java.util.Scanner; public class Average { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a series of numbers. Enter a negative number to quit."); double total = 0, num; int count = 0; while (true) { num = in.nextDouble(); if(num < 0) break; total += num; count++; } total /= count; System.out.println("You entered " + count + " numbers averaging " + total + "."); } }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.