0

我有一个部分工作的记录系统,但我在菜单界面上遇到了问题。基本上,菜单应该允许您:

  1. 添加学生
  2. 删除学生
  3. 打印学生
  4. 退出

现在菜单工作正常,但我在“添加学生”部分遇到问题。当他们添加学生时,他们需要:

  • 名字提示
  • 姓氏提示
  • 提示输入 ID 号

完成此操作后,它会询问他们是否要添加另一个;如果输入“y”,则再次循环,如果输入“n”,则返回主菜单。

现在在我的上,当他们按“n”时没有任何反应,当您按 y 时,它会再次循环,但它不会让您为名字输入任何值。

这是系统的所有代码:

REGISTRYINTERFACE.java

/**
 * @version 1.0
 * @author parry2411 
 */
package student;

import java.util.Scanner;

    public class RegistryInterface
    {

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

        public RegistryInterface(Registry theRegistry)
        {
        }

        public void doMenu()
        {
            boolean done = false;

            while (!done)
            {
                try
                {
                    System.out.println("\nRegistry Main Menu\n*******************\n");
                    System.out.println("1. Add a Student \n2. Delete a Student"
                            + "\n3. Print Registry\n0. Quit");

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

                    int decission = Integer.parseInt(userDecission);

                    switch (decission)
                    {
                        case 0:
                            System.out.println("System Closing Down..");
                            break;

                        case 1:
                            doAddStudent();
                            break;

                        case 2:
                            doDeleteStudent();
                            break;

                        case 3:
                            doPrintRegistry();
                            break;

                        default:
                            System.out.println("\nPlease Enter in a valid"
                                    + " Menu Option");
                            doMenu();
                            break;
                    }
                    done = true;
                }catch(Exception e)
                {
                    System.out.println("Incorrect Value Entered, Try Again....");
                }
            }
        }


    private void doAddStudent()
        {
            String addMore;
            do
            {
                System.out.println("\nAdd New Student\n***********\n");

                try
                {
                    System.out.println("Enter Students Forename :>");
                    String fName = input.nextLine();

                    System.out.println("Enter Student Surname :>");
                    String sName = input.nextLine();

                    System.out.println("Enter Student ID Number :>");
                    int idNum = input.nextInt();

                    theRegistry.addStudent(new Student(fName, sName, idNum));

                } catch (Exception e)
                {
                    System.out.println("\nERROR OCCURED: Incorect Value Entered"
                            + "\nTry Again... \nStudent Was NOT added");
                }


                System.out.println("\nAdd Another Student (Y/N) : >");
                addMore = input.next();



            } while (!"n".equals(addMore));
        }

        private void doDeleteStudent()
        {
            String another;
            do
            {
                System.out.println("\nDelete Student\n***********\n");
                System.out.println("Enter Student ID To Delete :>");
                try
                {
                    int studID = input.nextInt();
                    theRegistry.deleteStudent(studID);

                } catch (Exception e)
                {
                    System.out.println("\nERROR OCCURED: Incorect Value Entered"
                            + "\nTry Again...\n");
                }

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


            } while (!"n".equals(another));
        }

        private void doPrintRegistry()
        {

            System.out.println("\nPrinting Registry\n***********\n");

            if(theRegistry.studentList.size() == 0)
            {
                System.out.println("The Student Record System Contains No Student"
                        + " Records, Please Add Students\n\n");
            }
            else
            {
               System.out.println(theRegistry.format()); 
                System.out.println("\n****Printing Finished........");
            }

            doMenu();

        }
    }

注册表.java

/**
 * @version 1.0
 * @author parry2411 
 */
package student;

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"
                        + "\n Try Adding Again......");
                return;
            }
        }
        System.out.println("Student "+ aStudent.getForeName() + " "
                + "" + aStudent.getSurName() +" "
                + "Successfully Added To System.....");

        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();
    }
}

注册表应用程序

package student;

public class RegistryApp
{

    public static void main(String[] args)
    {
        Registry theRegistry = new Registry();
        RegistryInterface aRegInterface = new RegistryInterface(theRegistry);
        aRegInterface.doMenu();   
    }
}

学生.java

/**
 * @version 1.0
 * @author parry2411
 * Date Created: 18-Mar-2013
 */

package student;

public class Student 
{
    private String foreName;
    private String surName;
    private int studentID;


    public Student(String foreName, String surName, int studentID)
    {
        this.foreName = foreName;
        this.surName = surName;
        this.studentID = studentID;
    }

    public String getForeName()
    {
        return foreName;
    }

    public void setForeName(String foreName)
    {
        this.foreName = foreName;
    }

    public String getSurName()
    {
        return surName;
    }

    public void setSurName(String surName)
    {
        this.surName = surName;
    }

    public int getStudentID()
    {
        return studentID;
    }

    public void setStudentID(int studentID)
    {
        this.studentID = studentID;
    }

    @Override
    public String toString()
    {
        return getClass().getSimpleName()+"{" + "foreName=" + foreName + ", surName=" + surName + ", studentID=" + studentID + '}';
    }

    public String format()
    {
        return String.format("%-5s %-5s \t\t %-5d \n",foreName,surName,studentID);
    }
}

谢谢

4

1 回答 1

0

将您的方法更改doAddStudent为这种方式:

private void doAddStudent()
    {
        String addMore;
        do
        {
            System.out.println("\nAdd New Student\n***********\n");

            try
            {
                System.out.println("Enter Students Forename :>");
                String fName = input.next();//use next() instead of nextLine()

                System.out.println("Enter Student Surname :>");
                String sName = input.next();//use next() instead of nextLine();

                System.out.println("Enter Student ID Number :>");
                int idNum = input.nextInt();

                theRegistry.addStudent(new Student(fName, sName, idNum));

            } catch (Exception e)
            {
                System.out.println("\nERROR OCCURED: Incorect Value Entered"
                        + "\nTry Again... \nStudent Was NOT added");
            }


            System.out.println("\nAdd Another Student (Y/N) : >");
            addMore = input.next();



        } while (!"n".equals(addMore));
    }

由于Scanner.nextLine()您正在使用,您遇到了错误。现在问题是 为什么

Scanner.nextLine()如官方文档所述是:

将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。

Scanner.next()没有正确终止分配的内存行。因此,当在用户输入之后nextLine()调用 via时,它实际上终止了其中实际具有值的前一行 - 输入 via而不是获取新值。这就是它跳过来自 user 的条目的原因。因此,为了继续读取输入的值而不是前一个空行(因为返回的值不终止),您可以使用which 根据官方文档指出:String fName = input.nextLine();addMore = input.next();ynext()Stringnext()Scanner.next()

从此扫描器中查找并返回下一个完整的令牌。

更新
您还应该替换:

String userDecission = input.nextLine();

String userDecission = input.next();
于 2013-03-24T20:05:29.897 回答