【问题标题】:Where should I put the while loop/boolean?我应该把while循环/布尔值放在哪里?
【发布时间】:2019-11-16 04:50:21
【问题描述】:

我做了一个方法应该拉出一个带有动作列表的菜单,允许用户选择一个动作,执行该动作,然后返回菜单并重复,直到用户选择结束会话。我目前对如何处理 while 循环和 boolean endSession() 有点迷茫,我一直在代码中随意调整,有时让我编译它,但从不做我想要它做的事情。我目前拥有它的地方使它不仅跳过动作,而且无限循环。 我真的不需要知道到底出了什么问题,我只想知道将 while 循环/布尔 endSession 放在哪里(虽然没有必要,但解释一下为什么答案会起作用会很好)。

//package bookStoreLab;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class BookStoreDriver {
private static final int PRINT_INVENTORY=1;
private static final int SEARCH_TITLE=2;
private static final int SEARCH_AUTHOR=3;
private static final int AVAILABLE_BOOKS=4;
private static final int NEW_RELEASES=5;
private static final int PURCHASE=6;
private static final int ADD_INVENTORY=7;
//include rest of constants here for all choices
private static Scanner keyboard;

public static void main(String[] arg) throws IOException, FileNotFoundException{
    System.out.println("\f");
    keyboard = new Scanner(System.in);
    BookStore booky = new BookStore();
    System.out.println("*****" + 
    "Welcome To Your Local Book Store!" + "*****");
    booky.inputBooks();
    boolean endSession = false;
    int choice = getChoice(endSession);
    if(choice==0){
        endSession = true;
    }
    if(choice==PRINT_INVENTORY){
        booky.printInventory();
    }
    if(choice==SEARCH_TITLE){
        booky.searchByTitle();
    }
    if(choice==SEARCH_AUTHOR){
        booky.searchByAuthor();
    }
    if(choice==AVAILABLE_BOOKS){
        booky.printAvailableBooks();
    }
    if(choice==NEW_RELEASES){
        booky.printNewReleases();
    }
    if(choice==PURCHASE){
        booky.purchase();
    }
    if(choice==ADD_INVENTORY){
        booky.addToInventory();
    }
    else{
        System.out.println("Please choose an option that's actually listed.");
    }
    printFarewell();
}
public static int getChoice(boolean a){
    int choice=-1;
    while(!a){    
        System.out.println("\n\nWhat store service would you like" +
        "to perform? (Enter 0-7)");
        System.out.println("\t0. Enter 0 to end your session");
        System.out.println("\t1. View all books");
        System.out.println("\t2. Search by title");
        System.out.println("\t3. Search by author");
        System.out.println("\t4. View available books for purchase");
        System.out.println("\t5. View new releases");
        System.out.println("\t6. Purchase one or more copies of a book");
        System.out.println("\t7. Add book(s) to inventory");
        System.out.println("Enter your choice: ");
        choice = keyboard.nextInt();
    }
    return choice;
}
public static void printFarewell(){
    System.out.println("\n\nThank you for using the system!");
}

}

到目前为止,我已经尝试过: - 去掉 getChoice() 的参数 a 并将 while 循环移动到 if(choice==0) 之前并在 else 语句处结束

    while(!endSession){
if(choice==0){
        endSession = true;
    }
    if(choice==PRINT_INVENTORY){
        booky.printInventory();
    }
    if(choice==SEARCH_TITLE){
        booky.searchByTitle();
    }
    if(choice==SEARCH_AUTHOR){
        booky.searchByAuthor();
    }
    if(choice==AVAILABLE_BOOKS){
        booky.printAvailableBooks();
    }
    if(choice==NEW_RELEASES){
        booky.printNewReleases();
    }
    if(choice==PURCHASE){
        booky.purchase();
    }
    if(choice==ADD_INVENTORY){
        booky.addToInventory();
    }
    else{
        System.out.println("Please choose an option that's actually listed.");
    }
}

导致动作无限循环(例如:重复执行 printInventory() 直到我强制控制台停止)。

-将布尔endSession移动到getChoice()内部(无参数),这几乎可以工作,除了跳过执行操作的部分(例如:输入1,菜单立即弹出而不执行printInventory( ))。

【问题讨论】:

    标签: java while-loop boolean


    【解决方案1】:

    将while循环放在main方法中。您的布尔值在您的方法中永远不会改变,因此您的 while 循环将运行 0 次或无限次。

    让您的辅助方法收集单个输入,并让您的 main 方法中的循环检查最后返回的值并确定是否执行循环的另一次迭代。

    public static void main(String[] args) {
      int choice = getChoice(); 
      while(choice != 0) {
        choice = getChoice();
      }
    }
    
    public static int getChoice() {
      // display menu
      // collect user input
      // return user input
    }
    

    【讨论】:

    • 为了进一步扩展这一点,您的辅助方法,即您用于菜单的方法,应该这样做:菜单。它应该接受输入,然后执行正确的代码。循环应该在此之外,因为菜单实现也不会“如果用户退出则执行 x”。您的代码应该做的唯一事情是菜单和错误处理,在这种情况下是“else”语句,这是您正确完成的。只是想分享更多信息,以防您需要提醒。
    • 此外,您可能会对switch statements 产生更多兴趣,以清理您的 if 语句的可读性
    【解决方案2】:

    1) 去掉参数, 2) 将 while 循环放在 int choice = getChoice() 之前, 3) 在 else 循环之后结束它。

    我不知道为什么会这样,但我太累了,现在不在乎!

    【讨论】:

    • 没有“其他循环”之类的东西。