172k views
3 votes
Create a new Java project/class called Examine1. Prompt user as to how many numbers they would like to enter. Use a cumulative sum loop to read in and sum that many numbers. Once all numbers entered, program should print out the sum total and average of those numbers entered by the user. Use the printf command to format. Paste code.

User Toschneck
by
5.6k points

1 Answer

0 votes

Answer:

Step-by-step explanation:

The following code is written in Java and like requested prompts the user for a number to continue or a letter to exit. Then loops and keeps adding all of the numbers to the sum variable and adding 1 to the count for each number entered. Finally, it prints the sum and the average using printf and the variables.

import java.util.Scanner;

class Examine1 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int sum = 0;

int count = 0;

System.out.println("Enter a number to continue or a letter to exit.");

while(in.hasNextInt()) {

System.out.println("Enter a number:");

sum += in.nextInt();

count += 1;

}

System.out.printf("The sum is %s.%n", sum);

System.out.printf("The average is %s.", (sum / count));

}

}

Create a new Java project/class called Examine1. Prompt user as to how many numbers-example-1
User Aprok
by
4.0k points