2016-05-05 8 views
-3

私の教授が以下のようなプログラムをやりたいと思う瞬間、私はそれを達成するのが難しいです。私はプッシュするのを助けるために助けを求めています。ここに指示と私がこれまで持っているものがあります。クリシズムとアドバイスが受け入れられました!Javaプログラムto

1)最高得点と生徒名

2)最小スコアと生徒名

3)クラス

4の平均スコア)生徒名とその項目を表示マーク付きのスコアの高い順に表示されます。スコアが平均以上の場合は、「+」記号付きの マークです。彼らの得点が平均に等しい場合は、 '='で印を付ける。それ以外の場合は、 ' - 'でマークします。私は何をすべきかの

例イメージ: https://i.gyazo.com/403819a89bae613452dd8278b0612d81.png

私の問題で、私は最高のスコアを取得するための配列をソートし、それに行く対応する名前でそれをプリントアウトする方法がわかりません。 私はまた、1.名前のような数字を入れる方法も知らない。2.最初に0で始まるので名前。

私の仕事:

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

クラスを使用しよう! –

+1

ここで読んでください(http://stackoverflow.com/questions/5245093/using-comparator-to-make-custom-sort)。 'name'と' score'を格納する 'Student'クラスを作ることを強く勧めます。そうすれば、2つの異なるリストで順序を維持する必要はありません。 –

答えて

1

さてあなたはそこのクラスがあるので、あなたがそれらを使用して、学生のクラスを定義することができますJavaでは...
を与えられたこの種の問題のための良いアプローチを取っていません次のように:

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

あなたの業務を処理するためのクラスを作成することができ、学生のクラスで動作するように
またはあなたがこのような何かを行うことができます:

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 
}