We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
Sample Program
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) { // Create a Scanner object to read input Scanner scanner = new Scanner(System.in);
// Prompt the user to enter three integers
System.out.println("Enter the first number: "); int num1 = scanner.nextInt();
System.out.println("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.println("Enter the third number: ");
int num3 = scanner.nextInt();
// Calculate the average of the three integers
int sum = num1 + num2 + num3; double average = sum / 3.0;
// Display the average
System.out.println("The average of the three numbers is: " + average);
// Close the scanner
scanner.close(); } } Answer the questions below: 1.What data type would you use to store the three numbers in this program? 2.What method is used to prompt the user for input in a Java program? 3.How do you calculate the average of three integers in Java? 4.How do you display the average of the three numbers after calculating it? 5.What is the purpose of declaring three variables in this program? 6.What happens if the user enters a non-integer value when prompted for the three numbers?