code this in java using patterns: Specification: Read in integers until the user enters -1. If there was at least one integer, show the average value.
Question
code this in java using patterns: Specification: Read in integers until the user enters -1. If there was at least one integer, show the average value.
Solution
Sure, here is a simple Java program that reads integers from the user until they enter -1, then calculates and prints the average of the entered numbers.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
int count = 0;
int input;
System.out.println("Enter integers. Enter -1 to stop.");
while (true) {
input = scanner.nextInt();
if (input == -1) {
break;
}
sum += input;
count++;
}
if (count > 0) {
double average = (double) sum / count;
System.out.println("The average is: " + average);
} else {
System.out.println("No integers were entered.");
}
}
}
This program works as follows:
- It first creates a
Scannerobject to read the user's input. - It then enters a loop where it reads an integer from the user.
- If the user enters -1, it breaks out of the loop.
- Otherwise, it adds the entered number to the sum and increments the count of numbers.
- After the loop, if at least one number was entered (i.e., count > 0), it calculates the average by dividing the sum by the count and prints it.
- If no numbers were entered, it prints a message saying so.
Similar Questions
import java.util.Scanner;public class Test1 {public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); float number1 = input.nextInt(); float number2 = input.nextInt(); float number3 = input.nextInt(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); }}When a user enters 1 2 3, during the program execution what will be the exact output? Group of answer choices2.03.0The program gives an error1.0
In this exercise you will create a program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should display an appropriate error message if the first value entered by the user is 0.Hint: Because the 0 marks the end of the input it should not be included in the average.
Write a program that repeatedly asks for numbers. When the user enters 0, the program prints the average of all numbers entered (excluding the 0) accurate to two decimal places and exits.
Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.
Write a Java program that reads an unspecified number of scores anddetermines how many scores are above or equal to the average, and how many scoresare below the average. Enter a negative number to signify the end of the input. Assumethe maximum number of scores is 100.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.