【发布时间】: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