gamer3425
2013-04-14 02:20:54 UTC
Write a program that prompts the user to enter the number of students in a class, the student names and their scores on a test. The program should print the student names (along with their scores) in decreasing order of their scores (use the selection sort as covered in class). Your sort should be implemented as a method (please see the following method header):
Here is my code:
public class Assignment5 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the class: ");
int size = input.nextInt();
String[] names = new String[size];
double[] scores = new double[size];
for (int i = 0; i < size; i++) {
System.out.print("Please enter a student name: ");
input.useDelimiter(System.getProperty("line.separator"));
names[i] = input.next();
System.out.print("Please enter " + names[i] + "'s score: ");
scores[i] = input.nextDouble();
}
System.out.println("The class size is " + size);
SelectionSort(scores, names);
}
public static void SelectionSort (double[] score, String[] name) {
Scanner myInput = new Scanner(System.in);
for (int i = 0; i < score.length; i++) {
// This finds the minimum in the list.
double currentMin = score[i]; // Minimum = position in the list.
int currentMinIndex = i; // Index of the current minimum.
for (int j = i + 1; j < score.length; j++) {
if (currentMin < score[j]) {
currentMin = score[j];
currentMinIndex = j;
}
}
if (currentMinIndex != i) {
score[currentMinIndex] = score [i];
score[i] = currentMin;
}
}
System.out.println("Name Score");
for (int i = 0; i < score.length; i++) {
System.out.print(name[i] + score[i]);
System.out.println();
}
}
}
Now the problem is that when it does the sort, it doesn't put the grade with its associated score. What do I do to correct this problem?