-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

(我会将此作为评论发布,但我不确定它是否适合。)

以下是一些使您的程序更容易运行的建议:

分开你的声明。每个变量都有一个目的,并让标识符表明该目的。

在调试期间,使用使用固定种子初始化的 java.util.Random,而不是 Math.random。使用 Math.random,每次运行时都会得到不同的数字序列。

仔细查看输出。它做你没想到的事情的第一点在哪里?在上次一切都符合您的预期和第一次并非如此之间添加更多输出。

用纸和铅笔解决你的问题的小版本。

于 2012-11-06T04:05:19.040 回答
1

首先==,在比较字符串时切勿使用运算符,始终使用String.equals().:

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

第二。您正在传递超出您已读入的字符串数的最后一个索引。更改(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.

这个错误又犯了两次,改成:

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