java.util.scanner—尝试检查用户选择的id是否已在java程序中选择

qlfbtfca  于 2021-07-14  发布在  Java
关注(0)|答案(2)|浏览(363)

我正在做一个学校项目,基本上允许用户创建学生,班级,编辑他们,等等。我试图分配id给每个学生,用户创建,现在我允许用户输入自己的id和程序会比较它与其他id,如果id还没有被选择,完成,但如果它已经被选中,将显示一条消息,说它已经被选中,请选择一个不同的id,但我卡在这一部分,这里是我的代码,为简化起见,我只显示了主要和创建学生方法,我会对我的想法发表评论,希望你们能帮我一些想法,请看createstudent()方法。

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. Create Course" + "\n3. Create Enrollment" + "\n4. Edit Student" + "\n5. Edit Course"
                            + "\n6. Edit Enrollment" + "\n7. Display Student" + "\n8. Display Course" + "\n9. Display Enrollment"
                            + "\n10. Grades Sub Menu" + "\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) {
            CreateCourse();
        } else if(userInput == 3) {
            CreateEnrollment();
        } else if(userInput == 4 ) {
            EditStudent();
        } else if(userInput == 5) {
            EditCourse();
        } else if(userInput == 6) {
            EditEnrollment();
        } else if(userInput == 7) {
            DisplayStudent();
        } else if(userInput == 8) {
            DisplayCourse();
        } else if(userInput == 9) {
            DisplayEnrollment();
        } else if(userInput == 10) {
            GradesSubMenu();
        } 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) {
                CreateCourse();
            } else if(userInput == 3) {
                CreateEnrollment();
            } else if(userInput == 4 ) {
                EditStudent();
            } else if(userInput == 5) {
                EditCourse();
            } else if(userInput == 6) {
                EditEnrollment();
            } else if(userInput == 7) {
                DisplayStudent();
            } else if(userInput == 8) {
                DisplayCourse();
            } else if(userInput == 9) {
                DisplayEnrollment();
            } else if(userInput == 10) {
                GradesSubMenu();
            } else if(userInput == 0) {
                System.out.print("Done");
            }
        }

    }

    public static void CreateStudent() throws IOException {
        String FullName;
        String address;
        String city;
        String state;
        int newStudentID;
        boolean endOfFile = false;

        //Create a Scanner Object
        Scanner keyboard = new Scanner(System.in);

        //Open the file for write
        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);

        //Asks the user to enter their own ID
        System.out.print("Please get a Student ID(1-10): ");
        newStudentID = keyboard.nextInt();
        outputFile.writeInt(newStudentID);

        //Once all data is completed, close the file.
        outputFile.close();

        //Open the file for reading.
        FileInputStream team = new FileInputStream("StudentInfo.dat");
        DataInputStream inputFile = new DataInputStream(team);

        //My idea here was to create a for loop to iterate through the file
//to find the already selected IDs, if any of the IDs matches the ID selected by the user 
//then a message will be display to select a different ID, if no match, then done will be displayed.

        System.out.print("\ndone");

        inputFile.close();

    }
vltsax25

vltsax251#

您需要使用一致的编码样式。课程以大写字母开头;变量名以小写开头。两者都使用camel case。
像大多数初学者一样,您编写的代码太多。你太担心屏幕i/o了。
如果您已经了解了java集合,我建议您创建 CourseCatalog 以及 StudentRoster 类来容纳创建的类和学生。
这个 StudentRoster 可以很容易地确定学生id是否已经存在:

public class StudentRoster {

    private static int ID = 1;

    private Map<Integer, Student> roster = new TreeMap<>();

    public void addStudent(int id, Student s) { this.roster.put(id, student); }

    public void addStudent(Student s) { this.roster.put(ID++, student); }

    public boolean hasId(int id) { return this.roster.containsKey(id); }

    public Student getStudent(int id) { return this.roster.get(id); }

}
woobm2wo

woobm2wo2#

好的,您需要将id存储在一个集合中,并检查是否已经添加了此id。最简单的方法是在程序开始时读取输出文件,并将所有id保存到哈希集中:https://www.w3schools.com/java/java_hashset.asp 然后,当输入一个新的id时,您需要检查该id是否已经在hashset中。如果id存在,则打印错误,否则将学生添加到文件中,并将id添加到哈希集。
提示一下,您不应该使用以下代码:

if(userInput == 1) {
    CreateStudent();
} else if(userInput == 2) {
    CreateCourse();
} else if(userInput == 3) {
    CreateEnrollment();
} else if(userInput == 4 ) {
    EditStudent();
} else if(userInput == 5) {
   EditCourse();
} else if(userInput == 6) {
    EditEnrollment();
} else if(userInput == 7) {
    DisplayStudent();
} else if(userInput == 8) {
    DisplayCourse();
} else if(userInput == 9) {
    DisplayEnrollment();
} else if(userInput == 10) {
    GradesSubMenu();
} else if(userInput == 0) {
     System.out.print("Done");
} else {
    System.out.println("Invalid Option, Please try again.");
    userInput = keyboard.nextInt();
    if(userInput == 1) {
    ...

改用这样的方法:

int userInput = 0;
do {
    System.out.println("Please enter a valid choice(1-10, 0 to Quit) :");
    userInput = keyboard.nextInt();
    if(userInput == 1) {
        CreateStudent();
    } else if(userInput == 2) {
        CreateCourse();
    } else if(userInput == 3) {
        CreateEnrollment();
    } else if(userInput == 4 ) {
        EditStudent();
    } else if(userInput == 5) {
       EditCourse();
    } else if(userInput == 6) {
        EditEnrollment();
    } else if(userInput == 7) {
        DisplayStudent();
    } else if(userInput == 8) {
        DisplayCourse();
    } else if(userInput == 9) {
        DisplayEnrollment();
    } else if(userInput == 10) {
        GradesSubMenu();
    }
} while (userInput != 0);

当然,这也不是最好的方法,但我猜你只是一个初学者,所以不要走得太深,试图让你的项目以你的方式工作。

相关问题