Hey I posted earlier a few days ago about a program I am having trouble with. Well I have worked on it but still need a little bit of help. I have to write a program that will store 10 grades into an array. The program will need to output the lowest, highest, and average grade here is what I got so far. I am stuck and dont know how to get this to work. Thanks for any help.

//Arrayofscores.java

public class Arrayofscores
{
public static void main(String args[])
{
int [] scores = new int[10];
int smallest, highest,total=0;
double average =0.0;

//enter 10 scores
for (int i = 0;i<= scores.length-1;i++)
{
System.out.print("Enter Score " + (i+1) + ": ");
}

//lowest score
smallest = scores[0];

for (int i = 1; i <= scores.length-1;i++)
if (scores < smallest)
smallest = scores;

System.out.println("The lowest score is : " + smallest);

//highest score
highest = scores[0];

for (int i = 1; i <= scores.length-1;i++)
if (scores > highest)
highest = scores;

System.out.println("The highest score is : " + highest);

//average score
for (int i = 0; i<=scores.length-1;i++)
total = total + scores;

average = total/10.0;
System.out.println("\nThe average score is : " + average);

}

}

[B]Scanner scan = new Scanner(System.in);[/B]

//enter 10 scores
String score = "";
for (int i = 0;i<= scores.length-1;i++)
{
  try {

      System.out.println("Enter Score " + (i+1) + ": ");
      [B]score = scan.nextLine();[/B]
      scores[i] = Integer.parseInt(score);

  } catch(Exception e) {
       System.out.println("Value entered: "+score+" is not a number");
       System.exit(1);
  }

}

Edited 6 Years Ago by javaAddict: n/a

Ok so Im suppost to put this into my code and replace what I have? and how would you make a string of scores would they look like this? int[] gradeArray = {80,90,91,92,93,94,95,96,97,98};

The code you had didn't put any values into the array.
With the those 2 lines you can read input from the keyboard:

System.out.println("Enter Score " + (i+1) + ": ");
score = scan.nextLine();

You take the score, parse it into an int and put it into the array.

What you said is how you declare arrays that already have values:
int[] gradeArray = {80,90,91,92,93,94,95,96,97,98};

When you do this: scores[i] = Integer.parseInt(score); You put at the 'i' "element" of the array, the value you entered.
You read from the keyboard the grade as String (score variable) and then you turn it into an int:
Integer.parseInt(score) and put it into the array.

You need to take that part of the code and add it to your solution.

public class Arrayofscores
{
    public static void returnResult(int[] nums){
        double avg = 0;
        Arrays.sort(nums);
        System.out.println("Smallest Number is = "+nums[0]);
        System.out.println("Highest Number is = "+nums[nums.length-1]);
        for(int i=0; i<nums.length; i++)
            avg+= nums[i];
        avg = avg/nums.length;
        System.out.println("Average number is = "+avg);
    }

public static void main(String args[])
{
    int [] nums = {12,4,43,64,64,65,76,44,67,87,33,9};
    Arrayofscores.returnResult(nums);
} 

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.