我试图创建一个学生数据库系统,但我正在努力与界面菜单,当我按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 Menun*******************n");
System.out.println("1. Add a Student n2. Delete a Student"
+ "n3. Print Registryn4. 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 Studentn***********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 Studentn***********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 Registryn***********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();
}
}
在这个循环中…
while (decission != "4")
…您永远不会将新值设置为decission
。因此,循环要么无限运行,要么什么都不做。
另外,你从输入中扫描的字符串将始终是一个新创建的字符串,所以它将始终是!= "4",即使你输入了"4"。