[英]if-Else-if loop does not work
The requirement is : 要求是:
If the user chooses 1 the user will be asked to enter all the required values to create a LibraryItem and add it to the library 如果用户选择1,则将要求用户输入所有必需的值以创建LibraryItem并将其添加到库中
For choices 2 and 3 the user will be asked to enter the title of the LibraryItem 对于选项2和3,将要求用户输入LibraryItem的标题
For choices 4 and 5 the user will be asked to enter the barcode of the item The class InputReader may be used to get the users input. 对于选项4和5,将要求用户输入项目的条形码。InputReader类可用于获取用户输入。
This is my run method 这是我的跑步方法
/**
* Run the library
*
* @throws InvalidInputException
*/
public void run() throws InvalidInputException {
InputReader reader = new InputReader();
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
if (reader.readInt() == 1) {
System.out.println("Which catergory are you going to add?");
System.out.println("1. Music");
System.out.println("2. AudioBook");
System.out.println("3. PrintedBook");
if (reader.readInt() >= 1 && reader.readInt() <= 3) {
if (reader.readInt() == 1) {
System.out.print("Please enter a barcode: ");
String barcode = reader.readString();
System.out.println("Please enter a title: ");
String title = reader.readString();
System.out.println("Is it on loan? ");
boolean isOnLoan = reader.readBoolean();
System.out.println("What is the publishing year?");
int publishingYear = reader.readInt();
System.out.println("Who is the Artist?");
String artist = reader.readString();
System.out.println("What is the Playing Time?");
double playingTime = reader.readDouble();
System.out.println("What is the Sound Quality?");
String soundQuality = reader.readString();
Music music = new Music(title, barcode, isOnLoan,
publishingYear, artist, playingTime, soundQuality);
addItem(barcode, music);
System.out.println("Item added");
} else if (reader.readInt() == 2) {
System.out.print("Please enter a barcode: ");
String barcode = reader.readString();
System.out.println("Please enter a title: ");
String title = reader.readString();
System.out.println("Is it on loan? ");
boolean isOnLoan = reader.readBoolean();
System.out.println("What is the publishing year?");
int publishingYear = reader.readInt();
System.out.println("Who is the Author?");
String author = reader.readString();
System.out.println("Who is the publisher?");
String publisher = reader.readString();
System.out.println("What is the Playing Time?");
double playingTime = reader.readDouble();
AudioBook audioBook = new AudioBook(title, barcode,
isOnLoan, publishingYear, author, publisher,
playingTime);
addItem(barcode, audioBook);
System.out.println("Item added");
} else {
System.out.print("Please enter a barcode: ");
String barcode = reader.readString();
System.out.println("Please enter a title: ");
String title = reader.readString();
System.out.println("Is it on loan? ");
boolean isOnLoan = reader.readBoolean();
System.out.println("What is the publishing year?");
int publishingYear = reader.readInt();
System.out.println("Who is the Author?");
String author = reader.readString();
System.out.println("Who is the publisher?");
String publisher = reader.readString();
System.out.println("What is the number of pages?");
int numberPages = reader.readInt();
PrintedBook printedBook = new PrintedBook(title, barcode,
isOnLoan, publishingYear, author, publisher,
numberPages);
addItem(barcode, printedBook);
System.out.println("Item added");
}
} else if (reader.readInt() == 2) {
System.out.println("Please enter the Title: ");
String title = reader.readString();
searchByTitle(title);
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else if (reader.readInt() == 3) {
System.out.println("Please enter the Title: ");
String title = reader.readString();
removeTitle(title);
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else if (reader.readInt() == 4) {
System.out.println("Please enter the Barcode: ");
String barcode = reader.readString();
borrow(barcode);
System.out.println("Item Borrowed");
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else if (reader.readInt() == 5) {
System.out.println("Please enter the Barcode: ");
String barcode = reader.readString();
returnItem(barcode);
System.out.println("Item Returned");
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else {
return;
}
}
}
when i press 1 it always work but when i try to press 2 nothing show up until i have press 2 again then it work 当我按1时它总是可以工作,但是当我尝试按2时什么也没有出现,直到我再次按2时它才起作用
EDIT: 编辑:
this is the new code i done 这是我完成的新代码
public void printMainMenu() {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
public void printCatergory() {
InputReader reader = new InputReader();
int input = reader.readInt();
System.out.println("Which catergory are you going to add?");
System.out.println("1. Music");
System.out.println("2. AudioBook");
System.out.println("3. PrintedBook");
if (input >= 1 && input <= 3) {
if (input == 1){
System.out.print("Please enter a barcode: ");
String barcode = reader.readString();
System.out.println("Please enter a title: ");
String title = reader.readString();
System.out.println("Is it on loan? ");
boolean isOnLoan = reader.readBoolean();
System.out.println("What is the publishing year?");
int publishingYear = reader.readInt();
System.out.println("Who is the Artist?");
String artist = reader.readString();
System.out.println("What is the Playing Time?");
double playingTime = reader.readDouble();
System.out.println("What is the Sound Quality?");
String soundQuality = reader.readString();
Music music = new Music(title, barcode, isOnLoan,
publishingYear, artist, playingTime, soundQuality);
addItem(barcode, music);
System.out.println("Item added");
} else if (input == 2) {
System.out.print("Please enter a barcode: ");
String barcode = reader.readString();
System.out.println("Please enter a title: ");
String title = reader.readString();
System.out.println("Is it on loan? ");
boolean isOnLoan = reader.readBoolean();
System.out.println("What is the publishing year?");
int publishingYear = reader.readInt();
System.out.println("Who is the Author?");
String author = reader.readString();
System.out.println("Who is the publisher?");
String publisher = reader.readString();
System.out.println("What is the Playing Time?");
double playingTime = reader.readDouble();
AudioBook audioBook = new AudioBook(title, barcode, isOnLoan,
publishingYear, author, publisher, playingTime);
addItem(barcode, audioBook);
System.out.println("Item added");
} else {
System.out.print("Please enter a barcode: ");
String barcode = reader.readString();
System.out.println("Please enter a title: ");
String title = reader.readString();
System.out.println("Is it on loan? ");
boolean isOnLoan = reader.readBoolean();
System.out.println("What is the publishing year?");
int publishingYear = reader.readInt();
System.out.println("Who is the Author?");
String author = reader.readString();
System.out.println("Who is the publisher?");
String publisher = reader.readString();
System.out.println("What is the number of pages?");
int numberPages = reader.readInt();
PrintedBook printedBook = new PrintedBook(title, barcode,
isOnLoan, publishingYear, author, publisher,
numberPages);
addItem(barcode, printedBook);
System.out.println("Item added");
}
}
}
/**
* Run the library
*
* @throws InvalidInputException
*/
public void run() throws InvalidInputException {
InputReader reader = new InputReader();
printMainMenu();
int input = reader.readInt();
if (input >= 1 && input <= 6) {
if (input == 1) {
printCatergory();
} else if (input == 2) {
System.out.println("Please enter the Title: ");
String title = reader.readString();
searchByTitle(title);
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else if (input == 3) {
System.out.println("Please enter the Title: ");
String title = reader.readString();
removeTitle(title);
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else if (input == 4) {
System.out.println("Please enter the Barcode: ");
String barcode = reader.readString();
borrow(barcode);
System.out.println("Item Borrowed");
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else if (input == 5) {
System.out.println("Please enter the Barcode: ");
String barcode = reader.readString();
returnItem(barcode);
System.out.println("Item Returned");
System.out.println("Press '1' to return to Main Menu");
if (reader.readInt() == 1) {
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
}
} else {
return;
}
}
}
this happening because you are trying to take input every single time to fix that you have to change the code in the following manner 发生这种情况的原因是,您试图每次都输入信息以解决必须按以下方式更改代码的问题
System.out.println("1. Add LibraryItem");
System.out.println("2. Search a Title");
System.out.println("3. remove a Title");
System.out.println("4. Borrow an Item");
System.out.println("5. Return an Item");
System.out.println("6. Exit");
**int input =reader.readInt();**// add the input to a variable then reference this variable
if (input == 1)//like this
{
System.out.println("Which catergory are you going to add?");
System.out.println("1. Music");
System.out.println("2. AudioBook");
System.out.println("3. PrintedBook");
if (input) >= 1 && input <= 3) {
if (input == 1) {
System.out.print("Please enter a barcode: ");
You're problem is in this line: reader.readInt() >= 1 && reader.readInt() <= 3
. 您的问题是在这一行:
reader.readInt() >= 1 && reader.readInt() <= 3
。 This reads two values. 这将读取两个值。 To fix this, you should store the read value.
要解决此问题,您应该存储读取值。
int category = reader.readInt();
if(category >= 1 && category <= 3) {
//....
You do this quite often - you should store the values that are read and check those. 您经常这样做-您应该存储读取的值并检查它们。 Whenever you write
reader.readX()
, that's another input from the user. 每当您编写
reader.readX()
,这就是用户的另一种输入。
You should also clean up your code. 您还应该清理代码。 Make
print_menu
methods and it'll be a lot easier to debug. 制作
print_menu
方法,调试起来会容易print_menu
。
If you are going to individually check for each input value, you don't need 如果您要单独检查每个输入值,则不需要
if (input >= 1 && input <= 6) {
And a problem with your updated code is that you prompt for input before you print the options. 更新代码的问题是在打印选项之前提示您输入。 See here
看这里
public void printCatergory() {
InputReader reader = new InputReader();
int input = reader.readInt();
System.out.println("Which catergory are you going to add?");
System.out.println("1. Music");
System.out.println("2. AudioBook");
System.out.println("3. PrintedBook");
You should move the readInt
line beneath the System.out
您应该将
readInt
行移到System.out
public void printCatergory() {
InputReader reader = new InputReader();
System.out.println("Which catergory are you going to add?");
System.out.println("1. Music");
System.out.println("2. AudioBook");
System.out.println("3. PrintedBook");
int input = reader.readInt();
// if (input == 1) // do Music stuff
// else if (input == 2) // do AudioBook stuff
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.