Skip to content

Instantly share code, notes, and snippets.

@rebekah
Created October 2, 2023 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebekah/d1b7753da77a68b31ad0b8fc3c8038d4 to your computer and use it in GitHub Desktop.
Save rebekah/d1b7753da77a68b31ad0b8fc3c8038d4 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.ArrayList;
public class Metrics {
public static void main(String[] args) {
ArrayList<Integer> hieghtInInches = new ArrayList<Integer>();
ArrayList<Integer> weightInPounds = new ArrayList<Integer>();
String request = "Please add height(in inches) and weight(in pounds) for each " +
"person seperated by a colon. When you are done enter the letter Q.";
System.out.println(request);
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String metrics = sc.next();
if (metrics.contains("Q")) {
break;
}
if(!metrics.contains(":")){
System.out.println("Please make sure the height and weight are separated by a colon.");
System.out.println("Please reenter the height and weight");
continue;
}
String[] metricsList = metrics.split(":");
hieghtInInches.add(Integer.parseInt(metricsList[0]));
weightInPounds.add(Integer.parseInt(metricsList[1]));
System.out.println("Please add the next height and weight unless you are done, in which case enter a Q.");
}
int sumOfHeight = 0;
for (int num : hieghtInInches) {
sumOfHeight += num;
}
int averageHeight = sumOfHeight/hieghtInInches.size();
int maxWieght = 0;
for(int num: weightInPounds){
if(num > maxWieght){
maxWieght = num;
}
}
System.out.println(
String.format(
"The average height in inches is %d and the max weight in pounds is %d.", averageHeight, maxWieght
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment