0

我正在尝试创建一个学生数据库系统,但我在界面菜单上苦苦挣扎,当我为选项 1 按 1 时,没有任何反应,并且还有许多其他错误,例如无法打印。我究竟做错了什么?

下面是 RegistryInterface 的代码:

package student;

import java.util.Scanner;

public class RegistryInterface
{

    private Registry theRegistry = null;
    Scanner input = new Scanner(System.in);

    public RegistryInterface(Registry theRegistry)
    {
    }

    public void doMenu()
    {
        String decission;

        System.out.println("Registry Main Menu\n*******************\n");
        System.out.println("1. Add a Student \n2. Delete a Student"
                + "\n3. Print Registry\n4. Quit");

        System.out.println("Select Option [1, 2, 3, 4] :>");
        decission = input.nextLine();

        while (decission != "4")
        {
            if ("1".equals(decission))
            {
                doAddStudent();
            }

            if ("2".equals(decission))
            {
                doDeleteStudent();
            }


            if ("3".equals(decission))
            {
                doPrintRegistry();
                break;
            }

            if ("4".equals(decission))
            {

                System.out.println(" System Closing Down.........");
                break;
            }
        }
    }

    private void doAddStudent()
    {
        String addMore = null;

        String foreName;
        String surName;
        int iDNumber;

        while ("y".equals(addMore))
        {
            System.out.println("Add New Student\n***********\n");

            System.out.println("Enter forename       :>");
            foreName = input.nextLine();
            System.out.println("\nEnter surname      :>");
            surName = input.nextLine();
            System.out.println("Enter ID for Student :>");
            iDNumber = input.nextInt();

            theRegistry.addStudent(new Student(surName, foreName, iDNumber));

            System.out.println("Add Another Student(Y/N) :>");
            addMore = input.nextLine();
        }
    }

    private void doDeleteStudent()
    {
        int studID;
        String another = null;

        System.out.println("Add New Student\n***********\n");

        while("y".equals(another))
        {
            System.out.println("Enter Student ID To Delete :>");
            studID = input.nextInt();

            theRegistry.deleteStudent(studID);

            System.out.println("\nDelete Another? (Y/N)");
            another = input.nextLine();
        }
    }

    private void doPrintRegistry()
    {
        System.out.println("Printing Registry\n***********\n");
        theRegistry.format();
    }
}

这是registryApp的代码:

public class RegistryApp
{

    public static void main(String[] args)
    {
        Registry theRegistry = new Registry();

        RegistryInterface aRegInterface = new RegistryInterface(theRegistry);

        aRegInterface.doMenu();

    }
}

我的注册班:

import java.util.Iterator;
import java.util.LinkedList;

public class Registry
{
    public LinkedList<Student> studentList = new LinkedList<>();
    public Iterator<Student> iter = studentList.iterator();

    public Registry()
    {
    }

    public void addStudent(Student aStudent)
    {
        Iterator<Student> addIterator = studentList.iterator();

        while (addIterator.hasNext())
        {
            Student ob = addIterator.next();
            if (ob.getStudentID() == aStudent.getStudentID())
            {
                System.out.println("This Student ID "+ aStudent.getStudentID()+ " Is Already Used");
                return;
            }
        }
        System.out.println("Student "+ aStudent.getForeName() + " "
                + "" + aStudent.getForeName() +" "
                + "Successfully Added To System.....\n");

        studentList.addLast(aStudent);
    }

    public void deleteStudent(int studentID)
    {
        Iterator<Student> deleteIterator = studentList.iterator();
        boolean removed = false;

        while(deleteIterator.hasNext())
        {
            Student ob = deleteIterator.next();

            if(ob.getStudentID() == studentID)
            {
                deleteIterator.remove();
                removed = true;
                System.out.println(ob.getForeName() + " " + ob.getSurName() + " Was Succesffully Removed from System. \n");
            }
        }
        if(!removed)
        {
            System.out.println("Student ID not found");
        }
    }

    public String format()
    {
        StringBuilder sB = new StringBuilder();
        Iterator<Student> formatIterator = studentList.iterator();

        while(formatIterator.hasNext())
        {
            Student ob = formatIterator.next();
            sB.append(ob.format());      
        }
        return sB.toString();
    }

    @Override
    public String toString()
    {
        Iterator<Student> toStringIterator = studentList.iterator();
        StringBuilder sB = new StringBuilder();

        while(toStringIterator.hasNext())
        {
            Student ob = toStringIterator.next();
            sB.append(ob.toString()).append("\n");
        }
        return sB.toString();
    }



}
4

1 回答 1

0

在这个循环内...

while (decission != "4")

...您永远不会将新值设置为decission. 所以,循环要么无限运行,要么什么都不做。

此外,您从输入中扫描的字符串将始终是新创建的字符串,因此即使您键入“4”,它也将始终是!=“4”。

于 2013-03-21T19:48:24.043 回答