java - 我应该把 while 循环/boolean 值放在哪里?

标签 java while-loop boolean

我做了一个方法,应该拉出一个带有操作列表的菜单,允许用户选择一个操作,执行该操作,然后返回到菜单并重复,直到用户选择结束 session 。我目前对如何处理 while 循环和 boolean endSession() 有点迷失,我一直在代码中进行调整,有时让我编译它,但从来没有做我想要它做的事情。我目前拥有它的地方导致它不仅跳过 Action ,而且无限循环。 我真的不需要知道到底出了什么问题,我只是想知道在哪里放置 while 循环/boolean 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() 直到我强制控制台停止)。

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

最佳答案

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

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

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
}

关于java - 我应该把 while 循环/boolean 值放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58887683/

相关文章:

python - Python 2.x 中的 True=False 赋值

c - 带关系运算符的 while 循环

java - 如何让它在禁用所有按钮后只显示 Game Over 面板?

java - 使用 JndiObjectFactoryBean 在运行时设置动态模式

java - 为什么 90/270 度的仿射旋转不能保留正确的尺寸,而是将宽度和高度重新调整为相同的像素大小?

java - Java 是否允许和接受 hashCode 和 equals 方法中的异常?

java - 方法中的 SharedPreferences

c++ - 多次询问用户输入

php - PHP while循环中MYSQL更新和删除查询

javascript - 为什么当promise的async函数中有while循环时,JS会停止主线程的执行?