0

私は、1 人以上の学生の姓、名、スコアを受け取り、結果を配列に格納する学生スコア アプリケーションに取り組んでいます。次に、学生とそのスコアを姓のアルファベット順に出力します。学生が何人いるかはわかりませんが、100人未満になるでしょう。

生徒情報の最後にクラス平均を表示し、成績がクラス平均より 10 ポイント以上低い各生徒の後にメッセージを表示する必要があります。

私の最初の問題は、ユーザーが別の入力を希望するかどうかを尋ねる do/while ループを作成しましたが、機能しないことです!?!?

第二に、個々の学生に「10 ポイント以下」というメッセージを表示する方法がわかりません。

public class Student implements Comparable
{
    String firstName;
    String lastName;
    int score; 

    //stores last name, first name and score for each student
    public Student(String lastName,String firstName,int score)
    {
        this.lastName = lastName;
        this.firstName = firstName;
        this.score = score;    
    }
    //implement the comparable interface so students can be sorted by name
    public int compareTo(Object o)
    {
        Student otherStudent = (Student)o;

        if(otherStudent.lastName.equals(lastName))
            {
            return firstName.compareToIgnoreCase(otherStudent.firstName);
            }
        else
            {
            return lastName.compareToIgnoreCase(otherStudent.lastName);
            }
    }
    public String toString()
    {
        return lastName + ", " + firstName + ": " + score; 
    }
}

import java.util.Scanner;
import java.util.Arrays;

public class StudentApp
{
    static Scanner sc = new Scanner(System.in);

    public static void main(String [] args)
    {
        Student [] studentArray;
        String lastName;
        String firstName;
        int score = 0;
        double average = 0;


        System.out.println("Welcome to the Student Scores Application.");
        System.out.println();

        do{

            //code that uses variable to specify the array length
        int nStudent = 100;  //array size not set unit run time
        studentArray = new Student[nStudent];

            for (int i=0; i<nStudent; i++)
            {
            System.out.println();

            lastName = Validator.getRequiredString(sc, 
                           "Student " + (i+1) +  " last name: ");
            firstName = Validator.getRequiredString(sc, 
                           "Student " +  " first name: ");               
            score = Validator.getInt(sc, 
                         "Student " + " score: ",
                        -1, 101);

            studentArray[i] = new Student(lastName, firstName, score);

            double sum = 0.0;
            sum += score;
            average = sum/nStudent;
            }
        }while (getAnotherStudent());

        Arrays.sort(studentArray);

        System.out.println();

        for (Student aStudent: studentArray)
        {
            System.out.println(aStudent);
            if (score<= (average-10))
            {
                System.out.println ("Score 10 points under average");
            }
        }
        System.out.println("Student Average:" +average);
    }
    public static boolean getAnotherStudent()
    {
        System.out.print("Another student? (y/n): " );
        String choice = sc.next();
        if (choice.equalsIgnoreCase("Y"))
            return true;
        else
            return false;
    }
}
4

3 に答える 3

2

ここにはいくつかの問題があります。

  • do ... whileを実行するたびに、インスタンス化studentArrayしてsum。これは、trueの場合、以前に繰り返されたすべてのデータが無効になることを意味します。getAnotherStudent()配列をインスタンス化し、合計を1だけにします。
  • 100人以上の生徒がいても止まりません。nStudentループ内にも終了条件が必要です。
  • データをブロックできるようにいくつかの調整を行い、getAnotherStudent()有効なデータが入力されるのを待つ必要があります-ループを使用して:

     public static boolean getAnotherStudent() {
         Scanner sc = new Scanner(System.in);
         System.out.print("Another student? (y/n): " );
         if (sc.hasNext()) {  
             String choice = sc.next();
             // blocks here - ignores all input that isn't "y" or "n"
             while(!((choice.equalsIgnoreCase("Y") || choice.equalsIgnoreCase("N")))) {
                 if (choice.equalsIgnoreCase("Y")) {
                     return true;
                 }
                 System.out.print("Another student? (y/n): " );
                 choice = sc.next();
             }
          }
          return false; // obligatory
    
于 2012-05-28T03:06:17.397 に答える
1

あなたのコードは近いですが、いくつかの問題があります。do while ループが機能しない理由は、内部に for ループがあるためです。これは、別の生徒を追加するかどうかを尋ねる前に、100 人の生徒を要求することを意味します。合計はこのループ内で作成されるため、毎回リセットされます。

最後に、追加される生徒の数はわかりませんが、コードは 100 人の生徒がいると想定しています。これは、for each ループを使用して配列を通過できないことを意味します。一部は null になる可能性があります。追加した生徒の最後のインデックスまで、通常の for ループを使用するだけです。変更点は次のとおりです。

    Student[] student = new Student[nStudent]; 
    int studentCount = 0; //declear the counter outside the loop
    double sum = 0.0; //declear the sum outside the loop
    do {
        System.out.println();
        lastName = Validator.getRequiredString(sc, 
                       "Student " + (i+1) +  " last name: ");
        firstName = Validator.getRequiredString(sc, 
                       "Student " +  " first name: ");          
        score = Validator.getInt(sc, 
                     "Student " + " score: ",
                    -1, 101);

        student[studentCount] = new Student(lastName, firstName, score); 

        sum += score; //increase the sum

        studentCount++; //increment the counter

    } while (studentCount < nStudent && getAnotherStudent()); //stop if the user says 'n' or we hit the maximum ammount
    average = sum / studentCount; //work out the average outside the loop

    System.out.println();

    for (int i= 0; i< studentCount; i++ ) {
        System.out.println(aStudent);
        if (score <= (average - 10)) {
            System.out.println("Score 10 points under average");
        }
    }
    System.out.println("Student Average:" + average);
}
于 2012-05-28T02:53:01.413 に答える
-1

getAnotherStudent ()メソッドは次のようになります。

System.out.print("Another student? (y/n): " );
if (sc.hasNext()) {   // blocks until user entered something     
    String choice = sc.next();
            if (choice.equalsIgnoreCase("Y"))
                return true;
            else
                return false;
} else {
    // won't come here
    return false;
}
于 2012-05-28T02:47:58.193 に答える