Skip to content

Instantly share code, notes, and snippets.

@kaydell
Last active December 23, 2015 00:18
Show Gist options
  • Save kaydell/6552282 to your computer and use it in GitHub Desktop.
Save kaydell/6552282 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class AverageThreeInts {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int inputs = 2;
int[] values = new int[3];
while (inputs > -1) {
// It's a good idea to prompt for input before trying to get the next input from the user
System.out.println("Please enter an integer.");
values[inputs] = scanner.nextInt();
inputs--;
}
// It's also a good idea to give a clear message about what the output is
System.out.println("The averge of the integers entered is: " + averageValue(values));
}
private static int averageValue(int[] values) {
int sum = 0;
for (int i : values) {
sum += i;
}
return (sum / values.length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment