有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java这是我的最后一个代码,我在“if(Users[B].substring(0,1)==“1”){//Find String Cut Char Function”上遇到错误

如果您在查找如何解决此错误时遇到困难,请帮助我

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

共 (2) 个答案

  1. # 1 楼答案

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

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

    把你的声明分开。每个变量都有一个用途,并让标识符指示该用途

    在调试期间,使用java。util。用固定种子随机初始化,而不是数学。随机的数学。随机,你每次跑步都会得到不同的数字序列

    仔细观察输出。它做了你意想不到的事情的第一点在哪里?在上一次一切都如你所愿和第一次不如你所愿之间添加更多输出

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

  2. # 2 楼答案

    首先,在比较字符串时不要使用==运算符,始终使用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,当您尝试&;访问:

    Users[B].substring(0, 1)
    

    B = A

    此错误还会出现两次,请改为:

    for (A = 0; A < (NumTea - 1); A++) {