Reputation: 31
I was looking for some input on my code. What i'm trying to do is get user input in the form of scores on a test or something. The user can input how many grades they need to enter and it stores them on the array. Then the program will grade on a curve with the highest being an A. if a score is within 10 points of the highest score, it is also an A, if its in between 10 to 20 points less than the higher score its a B, if its 20 to 30 points less than the highest score then its a C, and so on till F.
My issue is that I don't know how to compare a certain element to the highest to see what grade it is. So my question is how do you compare a certain element in an array to the highest element in the array?
import java.util.Scanner;
public class StudentGrades {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int students;
System.out.println("How many students do you have?");
students = keyboard.nextInt();
while (students < 0) {
System.out.println("Invalid student amount");
students = keyboard.nextInt();
}
int[] kids = new int[students];
double[] scores = new double[students];
for (int index = 0; index < students; index++) {
System.out.println("Enter score for student #" + (index + 1) + ": ");
scores[index] = keyboard.nextDouble();
}
double high = scores[0];
for (int index = 1; index < scores.length; index++) {
if (scores[index] > high) ;
high = scores[index];
}
if (//place in array >= (highest-10)
System.out.println("Student (whatever there position is) has an A")
}
}
So if anyone can give me some insight into this issue I'll be grateful, also if you see anything wrong with the code please tell me. Any help would be great. Just to clarify my issue is with trying to find the letter grade. look at the if statement at the bottom.
Upvotes: 1
Views: 5464
Reputation: 15862
If I were you, I would do that in the following way:
public class Student implements Comparable{
int index;
private double grade;
public Student(int i, double d) {
grade = d;
index = i;
}
public int getIndex() {
return index;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
@Override
public int compareTo(Object o) {
Student s = (Student)o;
if (s.getGrade() < grade){
return -1;
} else if (s.getGrade() == grade) {
return 0;
}
return 1;
}
}
And the driver for the application is:
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
int amount;
System.out.println("How many students do you have?");
amount = keyboard.nextInt();
while (amount < 0) {
System.out.println("Invalid student amount");
amount = keyboard.nextInt();
}
Student[] students = new Student[amount];
for (int index = 0; index < amount; index++) {
System.out.println("Enter score for student #" + (index + 1) + ": ");
students[index] = new Student(index, keyboard.nextDouble());
}
Arrays.sort(students);
for (int i = 0; i < amount / 10 + 1; i++) {
System.out.println("Student " + (students[i].getIndex() + 1) + " has an A");
}
}
Edit:
additional clarification for the OP asked in comments:
double highest = students[0].getGrade();
for (Student s : students) {
if (s.getGrade() > highest) {
highest = s.getGrade();
}
}
Upvotes: 1