首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java菜单界面学生系统

Java菜单界面学生系统
EN

Stack Overflow用户
提问于 2013-03-22 03:39:56
回答 1查看 1.3K关注 0票数 0

我正在尝试创建一个学生数据库系统,但我正在为界面菜单而苦苦挣扎,当我为选项1按1时,什么也没有发生,还有许多其他错误,比如它不能打印。我做错了什么?

下面是RegistryInterface的代码:

代码语言:javascript
运行
复制
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的代码:

代码语言:javascript
运行
复制
public class RegistryApp
{

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

        RegistryInterface aRegInterface = new RegistryInterface(theRegistry);

        aRegInterface.doMenu();

    }
}

和我的register类:

代码语言:javascript
运行
复制
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();
    }



}
EN

回答 1

Stack Overflow用户

发布于 2013-03-22 03:48:24

在这个循环中。

代码语言:javascript
运行
复制
while (decission != "4")

..。您永远不会为decission设置新值。因此,循环要么无限运行,要么什么也不做。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15556560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档