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 に答える