/*
This program should allow the user to enter the age in years and weight in pounds of an indefinite
number of people. Have the user enter the ages and weights as integer values. Use a sentinal value
of -1 when you are done entering the information. Calculate the average weight and age for all
the people. Print the average age and weight of all the people to the DOS screen. Print to the DOS
screen the number of people on which information was entered. Print the highest age and the lowest
age of all the ages entered. Print the highest weight and the lowest weight of all the weights entered.*/
import java.util.*;
import java.text.DecimalFormat;
/*** Import Decimal Formating Class to format averages. ***/
public class People
{
public static void peopleData()/*Method for data*/
{
/*Variables*/
int people = 0;
double ageAverage = 0;
double weightAverage = 0;
int age;
int weight;
int ageTotal = 0;
int weightTotal = 0;
Scanner scannerObject = new Scanner(System.in);
/*Description of program for user.*/
System.out.println("The program should allow the user to enter the age in years");
System.out.println("and weight in pounds of an indefinite number of people.");
System.out.println("Enter the ages and weights as integer values.");
System.out.println("Use a sentinal value of -1 when you are done entering the information.");
System.out.println("");
System.out.println("\n\n");
System.out.println("Enter an age for a person and press ENTER:");
System.out.println("\n");
/** While statement to create loop for user. **/
age = scannerObject.nextInt();
while (age!= -2) /*While loop to enter. Enter -1 to exit */
{
ageTotal += age; /*Sums all the scores entered.*/
people ++; /* Counts the number of people entered */
System.out.println("Enter another age or -2 to enter weight.");
age = scannerObject.nextInt();
}/*End of while loop.*/
System.out.println("\n");
System.out.println("Enter weight or -1 to end");
ageAverage = ageTotal / people; /*Calculates average age.*/
weight = scannerObject.nextInt();
while (weight!= -1)
{
weightTotal += weight;
people ++;
System.out.println("Enter another weight or -1 to end");
weight = scannerObject.nextInt();
}
System.out.println("\n");
weightAverage = weightTotal / people;
DecimalFormat twoDigits = new DecimalFormat("0.00");
/** Create new DecimalFormat object named twoDigits **/
/*Displays number of people, total score and average score.*/
System.out.println("You entered " + people + " people.");
System.out.println("The sum of your age is " + ageTotal);
System.out.println("The sum of your weight is " + weightTotal);
System.out.println("Your average age is " + twoDigits.format(ageAverage));
System.out.println("Your average weight is " + twoDigits.format(weightAverage));
System.out.println("\n\n");
Scanner scan = new Scanner (System.in);
}/*End of method for data.*/
public static void main(String[] args)
{/*Main method.*/
People.peopleData();
}/*End of main method.*/
}/*End of class People*/
I don't know how to implement the highest value, lowest value for weight and age into the above so it calculates with my already compiled program. I need this to calculate the age and weight with my program above. Any help? Please.
Scanner scan = new Scanner (System.in);
int age = 0, lowest = 0, highest = 0;
for (int i = 0; i <; i++) {
System.out.println("Enter a number");
age = scan.nextInt();
if (i == 0) {
lowest = age;
highest = age;
} // end if
else if (age < lowest)
lowest = age;
else if (age > highest)
highest = age;

New Topic/Question
This topic is locked




MultiQuote





|