我应该在哪里放置while循环/布尔值?

时间:2019-11-16 04:50:21

标签: java while-loop boolean

我提出了一种方法,该方法应调出带有动作列表的菜单,允许用户选择一个动作,执行该动作,然后返回菜单并重复执行,直到用户选择结束会话为止。目前,我对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(),直到我强制控制台停止为止)。

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

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:-1)

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

我不知道为什么能行得通,但我太累了,不能照顾!