我做了一个方法,应该拉出一个带有操作列表的菜单,允许用户选择一个操作,执行该操作,然后返回到菜单并重复,直到用户选择结束 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/