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...
/* 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,...
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 ";   ...
/* 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...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
Write a C++ program that would report all of the Min and Max. example: 1. 95...
Write a C++ program that would report all of the Min and Max. example: 1. 95 2. 95 3. 80 4. 80 lowest 80 on test(s): 3, 4 highest 95 on test(s): 1, 2 I have started it, however, this only outputs one of the Min and Max and not all of the other instances. #include<iostream> using namespace std; int main() { int grades[10] , min , max , minIndex , maxIndex ; int n , choice , rt ,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT