for beginning java, here's my assignment " Write a program that ask the user to enter a list of positive scores until the user enters a negative score to terminate the input. You need to store these scores in an array. You can assume the maximum number of scores the user may enter is 50. However, you need to keep track of the actual number of scores entered. Write 2 methods:
for beginning java, here's my assignment "
Write a program that ask the user to enter a list of positive scores until the user enters a negative score to terminate the input. You need to store these scores in an array. You can assume the maximum number of scores the user may enter is 50. However, you need to keep track of the actual number of scores entered. Write 2 methods:
1. calculateAverage(): this method takes the list of scores and return the average score.
2. countPerfectScores(): this method takes the list of scores and return the number of perfect scores (100).
The main program reads the input and calls these methods and print the results.
Please make sure you write a comment line to document what your method does."
Here is my code, "
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Start array for scores
int[] scores = new int[50];
int size = 0;
// Have user enter scores until negative Score is entered
int score = sc.nextInt();
while (score >= 0 && count<50) {
scores[count] = score;
count++;
score = sc.nextInt();
}
}
// Calculate average score
double average = calculateAverage(scores, size);
System.out.println("Average: " + average);
// Calculate number of Perfect Scores
int perfect = calculatePerfect(scores, size);
System.out.println("Number of Perfect Scores: " + perfect);
}
/**
* Calculates the average of the scores
* @param scores - array of scores
* @param size - number of scores
* @return average - average of scores
*/
public static int calculatePerfect(int[] scores, int size) {
int perfect = 0;
for (int i = 0; i < size; i++) {
if (scores[i] == 100) {
perfect++;
}
}
return perfect;
}"
Why won't it run? Also, what am i missing from completing the problem?
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images