当前位置: 首页 > 知识库问答 >
问题:

如何给每个学生一个唯一的ID是由用户在Java程序中创建的

高茂
2023-03-14

我在Java做一个项目,用户输入信息,程序将数据写入二进制文件,你可以显示学生,你可以编辑学生,所有的工作都很好,唯一的东西我是缺失的,我被卡住了,是有一种方式,每次用户创建一个学生,该学生将被给予一个唯一的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");
        
        System.out.println("Please enter a valid choice(1, 0 to Quit) :");
        int userInput = keyboard.nextInt();
        
        if(userInput == 1) {
            CreateStudent();
        } 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 == 0) {
                System.out.print("Done");
            }
    public static void CreateStudent() throws IOException {
        String FullName;
        String address;
        String city;
        String state;
        
        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);
        
        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();
    }
    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();
    }

共有1个答案

吴浩皛
2023-03-14

根据代码,我假设这是一个高中或学习作业。那么最简单的方法就是读入文件,将最高的id存储在一个变量中。当用户添加一个新的student时,向该变量添加1并将其用作他的ID。

在更高级的应用程序中,您将使用UUID,它基本上是一个非常大的随机id,很难被复制。

 类似资料:
  • 问题内容: 我设置了一个向量集,以避免用迭代器分配之类的东西破坏GC(对于HashSet的值或键的每个遍历,对于集合引用和集合迭代器,您将分别获得一个新的/免费的) 无论如何,该方法是每个对象的唯一ID。(64位版本会失败吗?) 但是无论如何它都是可重写的,因此不能保证唯一,也不能保证每个对象实例唯一。 如果要创建“ ObjectSet”,如何为每个对象实例获取保证的唯一ID? 我刚刚找到了这个:

  • 问题内容: 我有以下SQLite代码。如何在每行中插入自动生成的唯一ID? 问题答案: 您可以将其定义为自动递增列: 正如MichaelDorner指出的那样,SQLite文档说an可以做同样的事情,并且速度稍快。该类型的列是其别名,其行为类似于自动增量列。 此行为是隐式的,可能会使经验不足的SQLite开发人员措手不及。

  • 创建一个表示学生的实体类 Student,其中有学生姓名、性别和年龄信息。要求使用属性来表示学生信息,最终编写测试代码。 首先定义一个名为 Student 的类,代码如下: 在类中通过属性定义学生、性别和年龄,代码如下: 在上述代码中将学生性别属性 Sex 设置为 private 作用域。为了对该属性进行获取和设置,还需要编写 isSex 和 setSex 方法。代码如下: 在 Student 类

  • 在上面的代码示例中,试图为每个请求生成唯一的txn-id。但是,观察到它正在重复使用相同的数字导致重复。我还尝试使用以下方法根据当前时间生成,当在1秒内触发超过1个请求时,它仍然会导致重复。 任何替代解决方案来为每个请求生成唯一ID,而不管并发情况如何? 提前感谢。

  • 问题内容: 我正在使用MS SQL Server Management Studio。我有桌子- 我想从该表创建另一个包含2列的表,以便column_1在Num_ID中提供唯一值(即1,2,3,4等),而column_2在Alpha_ID中提供唯一值(A,B,C等) 。 但是,如果已经出现一个字母,则不应再次出现。所以输出将是这样的- 希望这是有道理的。我想澄清一下,输入表中的ID不是我所显示的数

  • 所以我认为这里有2个场景: 每个地区(西欧除外)创建一个大型存储帐户,每个应用程序创建一个容器。存储帐户转到每个区域的资源组 1的退税: > 容器被设计为具有扁平结构(您可以创建文件夹,但这并不明显) 2的退税: 很多存储帐户 考虑到这一点,我想说为每个应用程序创建存储帐户是一个更好的选择,但也许我遗漏了一些东西。 所以问题是:在这里有其他的事情要比另一个更好的选择吗?也许是成本? 我偶然发现了一