我有一个部分工作的记录系统,但我在菜单界面上遇到了问题。基本上,菜单应该允许您:
- 添加学生
- 删除学生
- 打印学生
- 退出
现在菜单工作正常,但我在“添加学生”部分遇到问题。当他们添加学生时,他们需要:
- 名字提示
- 姓氏提示
- 提示输入 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);
}
}
谢谢