Here is a solution that uses two Scanner(as suggested in the previous answer).
Scanner stdin = new Scanner(System.in); scans user inputScanner scores = new Scanner(stdin.nextLine()); browses a row containing grades
Please also note that it uses a much simpler and more understandable formula for calculating the average value.
Scanner stdin = new Scanner(System.in);
System.out.print("Enter your average: ");
String name = stdin.next();
int count = 0;
int sum = 0;
Scanner scores = new Scanner(stdin.nextLine());
while (scores.hasNextInt()) {
sum += scores.nextInt();
count++;
}
double avg = 1D * sum / count;
System.out.print(name + " " + avg);
Output Example:
Enter your average: Joe 1 2 3
Joe 2.0
source
share