if-Else-if循环不起作用

时间:2015-11-30 20:50:20

标签: java loops

要求是:

  • 如果用户选择1,将要求用户输入所有必需的值以创建LibraryItem并将其添加到库中

  • 对于选项2和3,将要求用户输入LibraryItem的标题

  • 对于选项4和5,将要求用户输入项目的条形码 InputReader类可用于获取用户输入。

这是我的运行方法

/**
 * 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;
        }
    }
}

当我按1时 它总是有效但当我试着按2时什么也没出现 直到我再次按下2然后才能工作

编辑:

这是我完成的新代码

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;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

发生这种情况是因为您每次尝试输入以确定必须以下列方式更改代码

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: ");

答案 1 :(得分:1)

你的问题在于这一行:reader.readInt() >= 1 && reader.readInt() <= 3。这会读取两个值。要解决此问题,您应该存储读取值。

int category = reader.readInt();
if(category >= 1 && category <= 3) {
//....

你经常这样做 - 你应该存储读取的值并检查它们。每当你写reader.readX()时,那是用户的另一个输入。

您还应该清理代码。制作print_menu方法,调试起来会容易得多。

答案 2 :(得分:1)

如果您要单独检查每个输入值,则不需要

if (input >= 1 && input <= 6) {

更新代码的问题是您在打印选项之前提示输入。见这里

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");

您应该移动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
    // ...