Question

Write a program that asks the user to type in ages. They will type a negative...

Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline.

Sample output #1

Type a negative for age to exit
Enter your age: 21
Enter your age: 22
Enter your age: 21
Enter your age: -8
The average age is: 21.33
The oldest person is: 22 years old

Sample output #2

Type a negative for age to exit
Enter your age: -10
No ages were entered

This is what I have so far:

import java.util.Scanner;

public class Main
{
public static void main(String [] args) {
Scanner kb = new Scanner(System.in);
int age;
int max;
int count;
int avg;
System.out.println("Type a negative for age to exit");
System.out.print("Enter your age: ");
age = kb.nextInt();
  
avg = 0;
count = 0;
  
avg = age;
max = age;
  
while(age > 0) {
System.out.print("Enter your age: ");
age = kb.nextInt();
  
if(max < age) {
max = age;
}
if(age < 0) {
avg = avg + age;
count = count + 1;
}
if(count <= 0){
System.out.println("No ages were entered");
}
else {
System.out.printf("The average age is: %.2f\n", (1.0 * avg / count));
System.out.println("The oldest person is: " + max + " years old");
}
  
}
  
}
  

}

getting these errors:

1.

Input

-5

Your output

Type a negative for age to exit Enter your age:

Expected output

Type a negative for age to exit Enter your age: No ages were entered

2.

Input

95 -5

Your output

Type a negative for age to exit Enter your age: Enter your age: The average age is: 90.00 The oldest person is: 95 years old

Expected output

Type a negative for age to exit Enter your age: Enter your age: The average age is: 95.00 The oldest person is: 95 years old

Homework Answers

Answer #1

// NOTE: I have fixed the code

import java.util.Scanner;

public class Main
{
public static void main(String [] args) {
Scanner kb = new Scanner(System.in);
int age;
int max;
int count;
float avg;
int sum=0;
System.out.println("Type a negative for age to exit");
System.out.print("Enter your age: ");
age = kb.nextInt();
  
avg = 0;
count = 0;
  
avg = age;
max = age;
  
while(age > 0) {
if(max < age) {
max = age;
}
sum=sum+age;
count=count+1;
System.out.print("Enter your age: ");
age = kb.nextInt();
}
avg=(float)sum/count;
if(count == 0){
System.out.println("No ages were entered");
}
else {
System.out.printf("The average age is: %.2f\n", avg);
System.out.println("The oldest person is: " + max + " years old");
}
  
}
  

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
Write a Java program that asks the user to enter a person’s age. Then the program...
Write a Java program that asks the user to enter a person’s age. Then the program should display text indicating whether the person is an infant, a toddler, a child, a teenager, an adult, or a senior. It should display it just like this: “This person’s age category: x”, where x is the person’s age category based on the following guidelines: If less than 1 year old, the person is an infant. If at least 1 year old but younger...
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
Python 3 Conversion and exception handling Create a function that asks the user to enter a...
Python 3 Conversion and exception handling Create a function that asks the user to enter a number and returns the number. * function name: get_int * parameters: prompt (string) * returns: int * operation: Use the "prompt" parameter as the parameter to an input() call in order to ask the user for a number. Convert the number to a int using the int() function and return this number from the function. but - If the user enters something that cannot...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Write a program that takes two numbers from the Java console representing, respectively, an investment and...
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest...