Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created October 12, 2018 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save feehe21/95db94d50d1a9a28b9f513378ce7bdfb to your computer and use it in GitHub Desktop.
Save feehe21/95db94d50d1a9a28b9f513378ce7bdfb to your computer and use it in GitHub Desktop.
import java.util.*;
public class ArrayListAssignment {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//int y = scan.nextInt();
ArrayList<Integer> ages = new ArrayList<Integer>();
boolean keepAsking = true;
System.out.println("Type in at least 6 ages of you and your family. After 6 ages, type in a negative number to continue.");
int scanner = 0;
while(keepAsking == true){
scanner = scan.nextInt();
if(scanner < 0){
if(ages.size() >= 6){
keepAsking = false;
//System.out.println("spot 1");
}else{
System.out.println("You only have " + ages.size() + " ages entered. You need at least 6");
}
}else{
ages.add(scanner);
System.out.println("Ages added: " + ages.size());
}
}
//System.out.println("spot 2");
int average = 0;
int high = 0;
int highSpot = 0;
int low = ages.get(0);
int lowSpot = 0;
for(int i = 0; i < ages.size(); i++){
average += ages.get(i);
if(ages.get(i) > high){
high = ages.get(i);
highSpot = i;
}
if(ages.get(i) < low){
low = ages.get(i);
lowSpot = i;
}
}
//System.out.println("spot 3");
average /= ages.size();
System.out.println("You entered " + ages.size() + " ages, and the average age is " + average);
ages.remove(highSpot);
if (highSpot < lowSpot){
ages.remove(lowSpot - 1);
}else{
ages.remove(lowSpot);
}
average = 0;
//System.out.println("spot 4");
for(int i = 0; i < ages.size(); i++){
average += ages.get(i);
}
average /= ages.size();
System.out.println("Without the highest and lowest ages, there are " + ages.size() + " ages");
System.out.println("The new average is " + average);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment