如何在用户在JAVA程序上创建后为每个学生提供唯一的ID

如何在用户在JAVA程序上创建后为每个学生提供唯一的ID,java,java.util.scanner,Java,Java.util.scanner,我正在做一个学校项目,基本上允许用户创建、编辑或查看学生。一旦学生被创建,他们每个人都会被分配一个唯一的ID,如1、2、3等。所有的创建、编辑和显示功能都在发挥作用,但我一直坚持在创建后必须给他们一个唯一的ID。这是我的代码,//注释区域是我试图做的,但我不确定它是否正确。任何想法都将不胜感激 import java.util.Scanner; import java.io.*; public class MidTermProject { public static void main

我正在做一个学校项目,基本上允许用户创建、编辑或查看学生。一旦学生被创建,他们每个人都会被分配一个唯一的ID,如1、2、3等。所有的创建、编辑和显示功能都在发挥作用,但我一直坚持在创建后必须给他们一个唯一的ID。这是我的代码,//注释区域是我试图做的,但我不确定它是否正确。任何想法都将不胜感激

import java.util.Scanner;
import java.io.*;
public class MidTermProject {

    public static void main(String[] args) throws IOException {
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Here is the sample of menu choices for Main Menu.");
        
        System.out.println("\nWelcome to University Enrollment" + "\n1. Create Student" + "\n2. Edit Student" + "\n3. Display Student" + "\n0. --- Quit ---");
        
        System.out.println("Please enter a valid choice(1-10, 0 to Quit) :");
        int userInput = keyboard.nextInt();
        
        if(userInput == 1) {
            CreateStudent();
        } else if(userInput == 2) {
            EdithStudent();
        } else if(userInput == 3) {
            DisplayStudent();
        } else if(userInput == 0) {
            System.out.print("Done");
        } else {
            System.out.println("Invalid Option, Please try again.");
            userInput = keyboard.nextInt();
            if(userInput == 1) {
                CreateStudent();
            } else if(userInput == 2) {
                EditStudent();
            } else if(userInput == 3) {
                DisplayStudent();
            } else if(userInput == 0) {
                System.out.print("Done");
            }
        }

    }
    
    public static void CreateStudent() throws IOException {
        String FullName;
        String address;
        String city;
        String state;
        int StudentID;
        
        Scanner keyboard = new Scanner(System.in);
        
        FileOutputStream fstream =
                new FileOutputStream("StudentInfo.dat");
        DataOutputStream outputFile =
                new DataOutputStream(fstream);
        
        System.out.print("\nPlease enter your information bellow.\n" + "\nFull Name: ");
        FullName = keyboard.nextLine();
        outputFile.writeUTF(FullName);
        
        System.out.print("Address: ");
        address = keyboard.nextLine();
        outputFile.writeUTF(address);
        
        System.out.print("City: ");
        city = keyboard.nextLine();
        outputFile.writeUTF(city);
        
        System.out.print("State: ");
        state = keyboard.nextLine();
        outputFile.writeUTF(state);
        
        //allowed the user to select their own ID number
        System.out.print("Please get a Student ID(1-10): ");
        //Store the selected number on StudentID
        StudentID = keyboard.nextInt();
        //The for loop will increment index each time a user is created
        for(int index = 0; index == StudentID; index++) {
            //The if statement will compare index and StudentID, if equal will ask the user to enter a different number
            if(index == StudentID) {
                System.out.print("The selected ID has been selected already, Please select a different ID");
                StudentID = keyboard.nextInt();
            }
        }
        //write the number in the file
        outputFile.writeInt(StudentID);
        
        System.out.print("Successfully Created");
        
    }

    public static void EditStudent() throws IOException {
        String editName;
        String editaddress;
        String editCity;
        String editState;
        
        Scanner keyboard = new Scanner(System.in);
        
        RandomAccessFile file = 
                new RandomAccessFile("StudentInfo.dat", "rw");
        file.seek(0);
        
        System.out.print("\nPlease enter NEW information bellow.\n" + "\nFull Name: ");
        editName = keyboard.nextLine();
        file.writeUTF(editName);
        
        System.out.print("Address: ");
        editaddress = keyboard.nextLine();
        file.writeUTF(editaddress);
        
        System.out.print("City: ");
        editCity = keyboard.nextLine();
        file.writeUTF(editCity);
        
        System.out.print("State: ");
        editState = keyboard.nextLine();
        file.writeUTF(editState);
        
        file.close();
        
        System.out.print("Successfully Edited");
    }

    public static void DisplayStudent() throws IOException {
        FileInputStream fstream = new FileInputStream("StudentInfo.dat");
        DataInputStream inputFile = new DataInputStream(fstream);
        
        String student;
        boolean endOfFile = false;
        
        while(!endOfFile)
        {
            try
            {
                student = inputFile.readUTF();
                System.out.print(student + " ");
            }
            catch (EOFException e)
            {
                endOfFile = true;
            }
        }
        System.out.println("\nDone");
        
        inputFile.close();
    }
首先,以下内容使您的代码更具可读性。因此,在命名变量时首先使用小写字母:
studentId
,而不是
studentId
。首字母大写表示类名

在create student方法中,您的
for
循环毫无意义

for(int index = 0; index == StudentID; index++) {
    //The if statement will compare index and StudentID, if equal will ask the user to enter a different number
    if(index == StudentID) {
        System.out.print("The selected ID has been selected already, Please select a different ID");
        StudentID = keyboard.nextInt();
    }
}
for
循环只是增加第一个子句中定义的索引,根据第三个子句逐步增加,直到第二个子句证明
true

因此,在代码中,如果用户输入
4
,则循环计数为0、1、2、3、4«宾果»。现在我们达到了用户指定的号码,我们继续要求他们提供另一个号码。我们完成了。但这一过程是不合逻辑的

该代码无法实现检查现有学生的目标。你需要一个接一个地复习所有现有的学生。将每个现有学生ID与所需ID进行比较。只有在用尽所有已知学生的列表后,我们才应使用所需ID。如果我们在现有学生身上找到匹配项,则我们将打破循环,要求用户选择另一个ID。我们需要另一个外部循环来继续此“询问,搜索,如果需要,再次询问”处理,直到确定未使用的所需ID

大提示:如果你把你的问题陈述和解决方案尝试写成简单的散文,就像我刚才在上面一段中所做的那样,你的编程会进行得更顺利。你写的散文将成为在源代码中写有用评论的素材