I am trying to write code to store integers (scores), print them out, and then display the number of scores entered. However, if you enter 3 integers it displays the 3 you entered, 2 zero's, and then tells you you entered 4 scores?! Im very confused here and have tried various if statements, loops, decrementors, etc and I am still at a loss for a solution. This is the first part of a larger assignment.
Any help would be appreciated!!
import javax.swing.*;
public class Scores {
private final static int SIZE = 25;
public static void main(String[] args) {
int [] scores = new int[SIZE];
int count = 0;
double average = 0.0;
count = loadArray(scores);
average = determineAverage(scores, count);
displayResults(scores, count, average);
System.exit(0);
}
//returns the count of the scores entered
private static int loadArray(int[]n) {
int s = 0;
int i;
s = Integer.parseInt(JOptionPane.showInputDialog(null, "Input test score"));
if(s == -1){
break ;
} else {
n[i] = s;
i++;
}
//FYI returns the count plus one so that we include the zero position...
return(i+1); // so file compiles
}
//displays all of the results
private static void displayResults(int[] n, int cnt, double ave) {
for(int i = 0; i<=cnt; i++){
System.out.println(n[i]);
if(n[i]%10 == 0){
System.out.println();
}
}
System.out.println("The total number of test scores" + cnt);
// int outstanding = determineOutstanding(0);
//System.out.println("The number of Outstanding Scores" + outstanding);
}


Sign In
Create Account

Back to top









