当用户输入字符串时,按下输入时没有任何反应
问题描述:
我正在研究一个允许用户存储,借用和返回技术手册的库应用程序。当用户输入字符串时,按下输入时没有任何反应
构建应用程序的返回部分时,我遇到了问题。
目前,如果用户借用手册并希望返回它,则必须从显示的借用手册列表中输入手册的标题。但是,如果用户希望在该列表中的第一个之后返回任何手册,则什么都不会发生。用户输入名称并点击回车键,但应用程序停止。
这里是我的描述错误的例子:
下面是相关代码:
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.
*/
答
borrowsManual while循环中的p
需要递增,否则它将运行在无尽的lopp中。
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
}
(我不知道这整个线性搜索业务的那么好,但我们不希望重写整个应用程序,对不对?:)
答
你的方法不正确的返回类型。将返回类型更改为String,并将该行与Console.readString()放在for循环之外。希望有所帮助!
+1
'Console.readString()'不在循环中。 OP应该正确地格式化代码。 – PeterMmm
大概是告诉我们如何/在那里你用' returnManualTitle'并添加一些跟踪输出以查看问题出在哪里。 – PeterMmm
我没有看到你期望的字符串“请输入标题....”? – SMA
你如何得到输入? – SonOfSun