Java数组分级

时间:2016-11-07 19:26:53

标签: java arrays

我正在寻找代码的一些输入。我想要做的是在测试或其他东西上以分数的形式获得用户输入。用户可以输入他们需要输入的等级数,并将其存储在阵列上。然后程序将在最高得分为A的曲线上进行评分。如果得分在最高得分的10分之内,则它也是A,如果它比其得分B的低得多10到20分,如果它比最高分低20到30分,那么它是一个C,依此类推至F。

我的问题是我不知道如何将某个元素与最高元素进行比较以查看它的等级。所以我的问题是如何将数组中的某个元素与数组中的最高元素进行比较?

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")
    }

}

所以如果有人能给我一些关于这个问题的见解,我将不胜感激,如果您发现代码有任何问题请告诉我。任何帮助都会很棒。只是为了澄清我的问题是试图找到字母等级。看看底部的if语句。

1 个答案:

答案 0 :(得分:1)

如果我是你,我会用以下方式做到这一点:

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;
    }
}

该应用程序的驱动程序是:

   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");
        }
    }

修改
对OP的补充澄清在评论中提出:

double highest = students[0].getGrade();
for (Student s : students) {
    if (s.getGrade() > highest) {
        highest = s.getGrade();
    }
}