2016-04-20 5 views
0

このように、このプログラムでは、Array Listを使用する代わりにダイナミックアレイを使用してSchoolを構築しています。私は、メソッドを作成し、テストして、私はUIが動作するようにしようとしている以外はすべて動作します。UIループエラーです。続行または停止するためのUIオプションを取得できません。

UIは、生徒の配列を構築するためにユーザー入力を受けなければなりません。普通の学生とCSの学生。問題は、私が正規生またはCS生徒のいずれかに入学したいと思っている学生の数を入力すると、入学を希望する学生の数を入力した後に停止することです。

私は5人の学生を登録すると言うと、戻ってきて、もっと登録したり、学生を落としたり、メインメニューにリストされている他のオプションをしたいか尋ねられます。それが私の問題です。私はforループを追加しようとするたびに、またはif/else if文をコンパイルエラーの周りに切り替えようとしています。これは私が作成したコード全体です:

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

public class School { 
private Student[] theStudents; 

public School() { 
    this.theStudents = new Student[0];// needs to start out as empty 
} 

/* 
* next two methods would allow a user to add or drop a student into the 
* student array for the school ??Also with Enroll student, should be able 
* to assign a class to the student, i.e. Calculas, history, etc?? 
*/ 
public void enrollStudent(Student newStudent) { 
    Student totalStudents[] = new Student[theStudents.length + 1]; 
    for (int i = 0; i < theStudents.length; i++) { 
     totalStudents[i] = theStudents[i]; 
    } 
    totalStudents[theStudents.length] = newStudent; 
    theStudents = totalStudents; 
} 

public void dropStudent(String dropStudent) { 
    boolean checkForName = false; 
    for (int i = 0; i < theStudents.length; i++) { 
     if (theStudents[i].getName().equals(dropStudent)) { 
      theStudents[i] = null; 
      checkForName = true; 
     } 
    } 
    if (checkForName == true) { 
     Student totalStudents[] = new Student[theStudents.length - 1]; 
     for (int i = 0; i < totalStudents.length; i++) { 
      if (theStudents[i] == null) { 
       totalStudents[i] = theStudents[theStudents.length - 1]; 
      } else { 
       totalStudents[i] = theStudents[i]; 
      } 
     } 
     theStudents = totalStudents; 
    } 
    if (checkForName == false) { 
     System.out.println("The Student does not exist in the school"); 
    } 
} 

// add Test Score for a student 
public void addTestScore(String newStudent, double testScore) { 
    for (int i = 0; i < theStudents.length; i++) { 
     if (newStudent.equals(theStudents[i])) { 
      theStudents[i].addTestScore(testScore); 
     } 
    } 
} 

/* 
* count the number of students in a given class, not the school 
*/ 
public int countClassSize(String course) { 
    // Need to access how the course names are stored for the school to 
    // count this size. 
    int count = 0; 
    for (int i = 0; i < theStudents.length; i++) { 
     if (theStudents[i].getClassName().equals(course)) { 
      count = count + 1; 
     } 
    } 
    return count; 
} 

/* 
* get average average score of the student array The student array is made 
* up of Regular students and CompSciStudents. The average should take the 
* average of both the average score for Students and average score of 
* CompSciStudents and return the average average. 
*/ 
public double averageAverageScore(String course) { 
    double avgAvgTest = 0; 
    for (int i = 0; i < theStudents.length; i++) { 
     if (theStudents[i].getClassName().equals(course)) { 
      avgAvgTest += theStudents[i].getTestAvg(); 
     } 
    } 
    return avgAvgTest/this.countClassSize(course); 
} 

/* 
* add a programming language to only CS students at the school Will need to 
* use the instanceof for proper type casting 
*/ 
public void addProgrammingLanguage(String studentName, String programLanguage) { 
    for (int i = 0; i < theStudents.length; i++) { 
     if (this.theStudents[i] instanceof CompSciStudent) { 
      CompSciStudent tempStudent = (CompSciStudent) this.theStudents[i]; 
      tempStudent.learnedLanguage(programLanguage); 
     } 
    } 
} 

/* 
* Count the number of students in the school that know a certain 
* programming language, again will need to typecast properly 
*/ 
public int numberThatKnowLanguage(String programLanguage) { 
    int count = 0; 
    for (int i = 0; i < theStudents.length; i++) { 
     if (this.theStudents[i] instanceof CompSciStudent) { 
      CompSciStudent tempStudent = (CompSciStudent) this.theStudents[i]; 
      String knowThisLanguage = programLanguage; 
      tempStudent.learnedLanguage(knowThisLanguage); 
      count += 1; 
     } 
    } 
    return count; 
} 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    Scanner input = new Scanner(System.in); 
    String dropStudent, course; 

    System.out.println("Welcome to the School. Please select\n" + " Option 1 to enroll a regular student," 
      + "Option 2 to enroll a CompSci student, \n" + "Option 3 to drop a student, \n" 
      + "Option 4 to add test score or programming language, or \n" + "Option 5 to perform class analytics."); 
    int Operation = input.nextInt(); 

    /* 
    * Simple UI to add and drop students, will need to set the operation to 
    * call the add and drop students to fit them to the Student body array 
    * will need to make these two options loop until the user is satisfied 
    * with the size of the student body 
    */ 
    if (Operation == 1) { 
     System.out.println("Enter the # of regular students that you want to add to the school."); 
     int addStudents = input.nextInt(); 
     // Possibly create some type of input array to 
     // make it easier to enter the students' names. for(<type> <var 
     // name> : <array name>) 
     System.out.println("Please enter the name and course of the student you are enrolling:"); 
     for (int i = 0; i < addStudents; i++) { 
      String newRegularStudent = (String) input.next(); 
      course = input.next(); 
      System.out.println("Enter next student's name and then the course's name that he is enrolling in."); 

     } 
    } else if (Operation == 2) { 
     System.out.println("Enter the # of CompSci students that you want to add to the school."); 
     int addStudents = input.nextInt(); 
     /* 
     * Possibly create some type of input array to make it easier to 
     * enter the students' names 
     */ 
     System.out.println("Please enter the name and course of the student you are enrolling:"); 
     String newCompSciStudent = (String) input.next(); 
     course = input.next(); 
    } 

    else if (Operation == 3) { 
     System.out.println("Enter the # of students that you want to drop from the school."); 
     int dropStudents = input.nextInt(); 
     /* 
     * Possibly create some type of input array to make it easier to 
     * enter the students' names 
     */ 
     System.out.println("Please enter the name of the student you wish to drop from the school:"); 
     dropStudent = (String) input.next(); 

     /* 
     * After the first two operations, will need to build to the UI to 
     * call the other five methods, will need to make it into a 
     * while/for loop so user can continue to add information as needed. 
     */ 
    } 

    else if (Operation == 4) { 
     System.out.println("Enter the # for what you want to add to a student's records." 
       + "Enter 1 to enter a test score\n " + "Enter 2 to enter a programming language, enter 2."); 
     int optionNum1 = input.nextInt(); 
     /* 
     * Possibly create some type of input array to make it easier to 
     * enter the students' names 
     */ 
     if (optionNum1 == 1) { 

     } else if (optionNum1 == 2) { 

     } 
    } 

    else if (Operation == 5) { 
     System.out.println("This is the analytics section of this program.\n"); 

     System.out.println("Enter the # for which of the following analytics options that you want performed: " 
       + "Enter 1 to count the # of students for a particular class,\n " 
       + "Enter 2 to calculate the average average score for all students take a particular course, or\n " 
       + "Enter 3 to count the # of CompSciStudents."); 
     int optionNum2 = input.nextInt(); 
     if (optionNum2 == 1) { 
      System.out.println("Enter the course name that you\n" + " want to know the # of students for."); 
      course = input.next(); 
      Student classSize; 
     } 

     else if (optionNum2 == 2) { 
      System.out.println(
        "Enter the course name that you want to \n" + "calculate the average average test value for."); 
      course = input.next(); 
      Student testAvg; 
     } 

     else if (optionNum2 == 3) { 
      System.out.println("Count the # of CompSciStudents who know a particular programming\n" 
        + " language by entering that language name now."); 
      String programLanguage = input.next(); 
      CompSciStudent csStudent = null; 
      csStudent.knowsLanguage(programLanguage); 
     } 
    } 
    input.close(); 

} 
} 
+0

実行時に配列にサイズを与えることはできません。サイズは常に0のままです。ArrayListのようなものを使用してください。 –

+0

@kaanyılmazそれは答えでなければなりません。とにかく、カーンはまったく正しい。実際、 'ArrayList'は_DynamicArrays_を作成するJavaの方法です! ArrayListを使用したくないと言ったとしても、新しい 'DynamicArray'クラスをコーディングしない限り、他の可能性はありません。しかし、これはJavaのかなりのArray-Syntaxを削除しなければならないことを意味し、ランタイムパフォーマンスを悪化させます。 –

+0

@Krazor私はそれを答えとして加えています。 –

答えて

0

実行時にJavaの配列のサイズを変更することはできません。あなたが書いているので、あなたの配列のサイズは、それゆえ、常にゼロになります。

this.theStudents = new Student[0]; 

は、私はあなたが使用することをお勧め:あなたは今のArrayListを使用する方法がわからない場合は

ArrayLists<type> list = new ArrayLists<type>(); 

あなたはこのtutorialに従うことができます。

関連する問題