java - if-Else-if 循环不起作用

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

最佳答案

发生这种情况是因为您每次都尝试获取输入来修复您必须按以下方式更改代码

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

关于java - if-Else-if 循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34007754/

相关文章:

java - 如何比较不同循环的结果?

php - 在每个列表项后添加逗号,但仅当列表中有超过 1 个项目时才添加

java - 如何获取批量插入语句自动生成的键?

java - 递归二叉搜索树不打印Java中的数据

java - 2 个循环 1 个递减变量

javascript - 在 Javascript 中创建另一个数组出现次数的数组

c++ - 迭代器,循环导致段错误!为什么循环太多了?

java - 如何让方法返回几个不同对象之一,然后从该对象获取值?

java - 使用响应式(Reactive)消息传递时,Opentracing 上下文不会在 Quarkus 上传播

java - 以编程方式将文本颜色设置为二次色