-4

このエラーを解決する方法が見つからないので、助けてください。

import java.util.Scanner;
import java.io.*;
public class work {
    public static void main(String[] args) throws IOException {
        String Login;
        int A,B,C,D,NumTea,NumStu,PerClass,Teacher,Total,LoginID,LoginType,Choice;

        String[] Users = new String[20];

        Scanner FileScan;
        FileScan = new Scanner(new File("users.txt"));    
        A = 0;

        while (FileScan.hasNext())
        {
            Users[A] = FileScan.nextLine(); //Find the function to fix this
            A++;
        }

        Scanner reader = new Scanner(System.in);

        // Ready to get database of students and teachers
        // Arrays for seperating out students from teachers


        String[] Teachers = new String[1000];
        String[] Students = new String[1000];

        Total = A;

        System.out.println("Please wait for the system to intilize...");

        B=0;
        C=0;

        //Ready to seperate students from teachers in for loops

        for(B=0;B<=A;B++){
            if(Users[B].substring(0,1) == "1"){ //Find String Cut Char Function
                Students[C] = Users[B];
                C=C+1;
            }
        }

        NumStu = C+1;
        C=0;

        for(B=0;B<=A;B++){
            if(Users[B].substring(0,1) == "2"){ //Find String Cut Char Function
                Teachers[C] = Users[B];
                C=C+1;
            }
        }

        NumTea = C+1;

        System.out.println("Each teacher has 1 class a day but each student has 2 classes per day");
        System.out.println("There are " + NumTea + " teachers in the school therefore there are " + (NumTea) + " classes");
        System.out.println("There are " + NumStu + " students in the school therefore there are " + ((NumStu*2)/NumTea) + " kids per class on average"); //Diving correctly?

        PerClass = ((NumStu*2)/NumTea);

        int[][] StudentClass = new int[NumStu][2]; //Looks at what classes each student has
        int[][] TeacherClass = new int[NumTea][1000]; //Looks at what students each teacher has

        for(B=0;B<=(NumStu-1);B++){
            Teacher = (int)(Math.random()*NumTea); //Import Math Library and check random function
            StudentClass[B][0] = Teacher; //Assigning first class
            Teacher = (int)(Math.random()*NumTea);
            StudentClass[B][1] = Teacher; //Assigning second class
            if(StudentClass[B][0] == StudentClass[B][1]){  //Error checking for same class
                Teacher = (int)(Math.random()*NumTea);
                StudentClass[B][1] = Teacher;
            }
        }

        //Ready to look at student classes and assign them to teachers

        B=0;
        C=0;
        D=0;

        int[] StuClass = new int[NumTea]; //Looks at how many students are in each class

        for(B=0;B<=(NumTea-1);B++){
            D=0;
            for(C=0;C<=(NumStu-1);C++){
                if(StudentClass[C][0] == B || StudentClass[C][1] == B){
                    TeacherClass[B][D] = C;
                    D=D+1;
                }
            }
            StuClass[B] = D;
        }

        System.out.println("Classes print out:");

        // Going to print out all students and all teachers in each class

        A=0;
        B=0;

        for(A=0;A<=(NumTea-1);A++){
            System.out.println(Teachers[A] + "'s class (" + (StuClass[A]+1) + "):");
            for(B=0;B<=(StuClass[A]);B++){
                System.out.println("           " + Students[(TeacherClass[A][B])]);
            }
        }

        while(1==1){

            System.out.println("Welcome to Wayzata WHS's SGS (Scheduling and Grading System)");
            System.out.println("Login:");

            Login = reader.nextLine();

            A = 0;
            LoginType = 0;
            LoginID = 0;

            for(A=0;A<=(NumStu-1);A++){
                if(Students[A].toLowerCase() == Login.toLowerCase()){ //Check to see if string lower is correct
                    System.out.println("Found your account, linking you with the system...");
                    LoginID = A;
                    LoginType = 1;
                }
            }

            for(A=0;A<=(NumTea-1);A++){
                if(Teachers[A].toLowerCase() == Login.toLowerCase()){ //Check to see if string lower is correct
                    System.out.println("Found your account, linking you with the system...");
                    LoginID = A;
                    LoginType = 2;
                }
            }

            //The user has typed in a name and the system has recognized it and found it in the system the program has logged ethier a student or teacher in

            if(LoginType == 1){
                System.out.println("Welcome " + Students[LoginID] + "! Signed in as: STUDENT");
                System.out.println("What would you like to do today?");
                System.out.println("1: See grades");
                System.out.println("2: Logout");
                Choice = reader.nextInt();

                if(Choice == 1){
                    System.out.println("1: " + Teachers[(StudentClass[LoginID][0])] + "'s Class");
                    System.out.println("2: " + Teachers[(StudentClass[LoginID][1])] + "'s Class");
                    Choice = reader.nextInt();
                    //c.PrintClassGrades(LoginID,Choice); //Add a class for this
                }
            }else if(LoginType == 2){
                System.out.println("Welcome " + Teachers[LoginID] + "! Signed in as: TEACHER");
                System.out.println("What would you like to do today?");
                System.out.println("1: Do attendance");
                System.out.println("2: Look at class grades");
                System.out.println("3: Add grade to gradebook");
                System.out.println("4: Logout");
                Choice = reader.nextInt();

                if(Choice == 1){
                    //c.Attendance(LoginID); //Add a class for this
                }else if(Choice == 2){
                    //c.ClassGrades(LoginID); //Add a class for this
                }else if(Choice == 3){
                    //c.AddGrade(LoginID);//Add a class for this
                }
            }
        }
    }
}
4

2 に答える 2

2

(これをコメントとして投稿しますが、それが適切かどうかはわかりません。)

プログラムをより簡単に動作させるためのいくつかの提案を以下に示します。

宣言を分離します。変数ごとに 1 つの目的を持ち、その目的を示す識別子を持たせます。

デバッグ中は、Math.random ではなく、固定シードで初期化された java.util.Random を使用してください。Math.random を使用すると、実行するたびに異なる数列が得られます。

出力を注意深く見てください。予期しないことを最初に実行したポイントはどこですか? 前回すべてが期待どおりだったときとそうでなかった最初のときの間に出力を追加します。

紙と鉛筆を使って、問題の小さなバージョンを作成します。

于 2012-11-06T04:05:19.040 に答える
1

まず==文字列を比較するときに演算子を使用しないでください。常にString.equals().:を使用してください。

if ("1".equals(Users[B].substring(0, 1))

2番。読み込んだ文字列数の最後のインデックスを超えて渡しています。変更 (x2):

for (B = 0; B <= A; B++) {

for (B = 0; B < A; B++) {

配列内のすべてのエントリ:

String[] Users = new String[20];

nullデフォルトで初期化され、NullPointerExceptionアクセスしようとすると次のようになります。

Users[B].substring(0, 1)

いつB = A

この間違いがさらに 2 回行われている場合は、次のように変更します。

for (A = 0; A < (NumTea - 1); A++) {
于 2012-11-06T01:31:48.517 に答える