109k views
4 votes
JAVA

Write a program that will input a list of test scores in from the keyboard. When the user enters -1, print the average.
What do you need to be careful about when using-1 to stop a loop?
Sample Run:
Enter the Scores:
45
100
-1
The average is: 72.5

User Ssegvic
by
5.8k points

1 Answer

2 votes

import java.util.Scanner;

public class JavaApplication35 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the Scores:");

int total = 0;

int count = 0;

while (true){

int num = scan.nextInt();

if (num == -1){

break;

}

else{

total += num;

count += 1;

}

}

System.out.println("The average is: "+((double)total/count));

}

}

I hope this helps!

User Oleg Ananiev
by
5.7k points