Question: How do I format the output for average age to round to the nearest 10th? Also, how do I get the oldest age to be
How do I format the output for average age to round to the nearest 10th? Also, how do I get the oldest age to be correct? (Java)
package cis254assignments;
import java.util.Scanner;
public class A6_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int AGE_GROUP_A = 18; // age limit set to 18
final int AGE_GROUP_B = 30; // age limit set to 30
final int AGE_GROUP_C = 40; // age limit set to 40
final int AGE_GROUP_D = 60; // age limit set to 60
int age = 0;
int ageGroupA = 0; // variable to store count of age group 0-18
int ageGroupB = 0; // variable to store count of age group 19-30
int ageGroupC = 0; // variable to store count of age group 31-40
int ageGroupD = 0; // variable to store count of age group 41-60
int ageGroupE = 0; // variable to store count of age group over 60
int sumAges = 0;
int attendeeCount = 0;
int youngest = 0;
int oldest = 0;
int ave;
int popcorn = 0;
int soda = 0;
int both = 0;
String preference;
System.out.print("Enter age of attendee (negative number to quit): ");
age = input.nextInt();
while (age >= 0) {
sumAges += age;
attendeeCount++;
youngest = age;
oldest = age;
System.out.print("food preference (enter 'p' fpr popcorn, 's' for soda, 'b' for both): ");
preference = input.next();
if (age < youngest)
youngest = age;
else if (youngest < age);
ag;
if (age <= AGE_GROUP_A)
ageGroupA++;
else if (age <= AGE_GROUP_B)
ageGroupB++;
else if (age <= AGE_GROUP_C)
ageGroupC++;
else if (age <= AGE_GROUP_D)
ageGroupD++;
else if (age > AGE_GROUP_D)
ageGroupE++;
if (preference.equals("p"))
popcorn++;
else if (preference.equals("s"))
soda++;
else if (preference.equals("b"))
both++;
System.out.print("Enter age of attendee "
+ "(negative number to quit): ");
age = input.nextInt();
}
System.out.println("age 0 to 18: " + ageGroupA);
System.out.println("age 19 to 30: " + ageGroupB);
System.out.println("age 31 to 40: " + ageGroupC);
System.out.println("age 41 to 60: " + ageGroupD);
System.out.println("over 60: " + ageGroupE);
System.out.println();
System.out.println("Food preference popcorn: " + popcorn);
System.out.println("Food preference soda: " + soda);
System.out.println("Food preference both: " + both);
System.out.println("The average age was " + sumAges / attendeeCount);
System.out.println("The youngest person in attendance was: " + youngest);
System.out.println("The oldest person in attendace was: " + oldest);
{
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
