-3

At the moment my professor wants to do a program like below, I'm having difficulty achieving that, I'm seeking help to help me push through. Here is the instructions and what I have so far. Cristisim and advice accepted!

1) The highest score and the student name

2) The lowest score and the student name

3) Average score of the class

4) Display the student names and their scores in decreasing order of their scores with marks: if their score is above average, mark with a ‘+’ sign; if their score equals to average, mark with a ‘=’; otherwise, mark with a’-‘.

Example Image of what I have to do: https://i.gyazo.com/403819a89bae613452dd8278b0612d81.png

My problems are, I don't know how to sort the arrays to get the highest score and print it out with the corresponding name that goes with it. I also don't know how to put the numbers like 1. Name, 2. Name because it starts with 0 first.

My work:

        public static void main(String[] args) {
        System.out.println("********** Students and Scores ************");
        Scanner input = new Scanner(System.in);
        System.out.print("How many students in your class?: ");
        int numStudents = input.nextInt();
        double[] scores = new double[numStudents];
        String[] students = new String[numStudents];
        for (int i = 0; i < numStudents; i++) {
            System.out.print(i + ". Name: ");
            students[i] = input.next();
            System.out.print("Score: ");
            scores[i] = input.nextDouble();

        }

        double sum = 0;
        for(int i=0; i < scores.length ; i++)
                sum = sum + scores[i];
        double average = sum / scores.length;

        System.out.println("*************** Results *****************");
        System.out.println("Average: " + average);
        System.out.println("******* End of Students and Scores *********");
}
Joshua
  • 5
  • 4
  • 1
    Read up here (http://stackoverflow.com/questions/5245093/using-comparator-to-make-custom-sort). I would highly suggest making a `Student` class that stores the `name` and the `score` so you don't need to maintain order in two different lists. – Colin Basnett May 05 '16 at 20:48

1 Answers1

1

Well you have not taken the good approach for this kind of given problem...
In Java there are classes, so you can use them and define your Student class like following:

public class Student {
    //variables
    private String name;
    private double score;

    //constructor
    public Student(String name, double score) {
        this.name = name;
        this.score = score;
    }

    //getters and setters
    public String getName() {
        return this.name;
    }

    public double getScore() {
        return this.score;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setScore(double score) {
        this.score = score;
    }
}

You can create classes to handle your operations and to work with student class
or you can do something like this:

public static void main(String[] args) {
    List<Students> students = new ArrayList<Students>();
    students.add("Jhon", 10);
    students.add("Mary", 5.5);
    students.add("Ana", 3);

    Iterator i = students.iterator();

    //iterate through your students list like tihs;
    while(i.hasNext()) {
        System.out.println("Student = "+ i.next());
    }
   // do what ever you want with the list 
   // make your calculations and stuff, what your problem required
}
V. Sambor
  • 12,361
  • 6
  • 46
  • 65