当用户输入字符串时,按回车键没有任何反应

When user inputs string, nothing happens upon pressing enter

我正在开发一个图书馆应用程序,它允许用户存储、借阅和 return 技术手册。

我在构建应用程序的 return 部分时遇到了 运行 问题。

目前,如果用户借用手册并且他们希望 return 它,他们必须从显示的借用手册列表中输入手册的标题。但是,如果用户希望 return 该列表中第一个之后的任何手册,则不会发生任何事情。用户输入名称并按下回车键,但应用程序只是停止。

这是我描述的错误示例:

相关代码如下:

static void returnManual(){
    System.out.printf("\n\nHere are the Manual/s currently out on loan:\n\n");
    if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && borrowedManuals.size() >= ManualChoice){
        for (int i = 0; i < borrowedManuals.size(); i++)
            System.out.println(borrowedManuals.get(i).displayManual());
        returnManualTitle = Console.readString(Messages.enterManualTitle, Messages.tooShortMessage, 3);
    }

    int x = 0;
    boolean titleExistance = false;
    while (x < ManualList.size()){//Search for the Manual by title, if it exists change it's status,
                                //it's borrower and borrowDate.

        if (ManualList.get(x).title.equalsIgnoreCase(returnManualTitle)){

            ManualList.get(x).status = "Available";
            ManualList.get(x).borrower = "N/A";
            ManualList.get(x).borrowDate = "N/A";
            ManualList.get(x).returnDate = "N/A";

            int p = 0;
            borrowLoop:
            while (p < borrowedManuals.size()){//Search for the Manual by title, if it exists change it's status,
                //it's borrower and borrowDate.

                if (borrowedManuals.get(p).title.equalsIgnoreCase(returnManualTitle)){

                    borrowedManuals.remove(p);
                    break borrowLoop;
                }

            }               
            System.out.println(Messages.successReturnMessage);
            titleExistance = true;

            break;//if a title is found, break out of the loop and display choice menu.
        }
        x = x+1;
    }//end of while loop.
    if(titleExistance == false){
        boolean repeatReturnManual = Console.readYesNo("\n--------------------------------------------------------------------------" + "\n\nThe Manual with the title "+"\""+returnManualTitle +"\""+ " wasn't found!"
                                                        +"\n\nDo you want to try again? (Y/N):\n");
        System.out.println("\n--------------------------------------------------------------------------");
        if(repeatReturnManual){
            returnManual();
        }else{
            Menu.displayMenu();
        }
    }else if(titleExistance){
        Menu.menuChoice = 7;
    }               
}

/**
 * Removes the Manual.
 */

您的方法没有正确的 return 类型。将 return 类型更改为 String 并将带有 Console.readString() 的行放在 for 循环之外。希望对您有所帮助!

borrowsManual while循环中的p需要自增,否则会运行无限循环

while (p < borrowedManuals.size()) {
    Manual borrowed = borrowedManuals.get(p); // guessing the name of this class
    if (borrowed.title.equalsIgnoreCase(returnManualTitle)) {
        borrowedManuals.remove(p);
        break;
    }
    p++; // this is mising
}        

(我不确定整个线性搜索业务是否如此出色,但我们不想重写整个应用程序,对吗?:)