0

想知道是否有人可以帮助我解决我的问题?无论如何,我在 java 中是否可以为用户创建一个允许他们更改 displayOptions 方法中的选项的提示?基本上我想做的是改变出口来完成,如果这有意义的话?

import java.util.Scanner;

public class part3 {


        private static void displayOptions() {
            System.out.println("Choose your option:");
            System.out.println("**** West End Theatre Booking System ****");
        System.out.println("=========================================");
        System.out.println("1. List all shows and availability");
        System.out.println("2. Book and print ticket(s)");
        System.out.println("3. Cancel a ticket");
        System.out.println("4. Display theatre/show seating map");
        System.out.println("5. Show income/accounts");
        System.out.println("6. Assign new show to theatre");
        System.out.println("7. Exit");
    }

    //--------------------------------------------------


    private static int getUserOption(String prompt) {
        int option;
        Scanner k=new Scanner(System.in);
        System.out.print(prompt);
        option=k.nextInt();
        return option;
    }
    //-------------------------------------------------


    public static void main(String[] args) {

        int selection,wick,oli,phant,wickremain,oliremain,phantremain;
        selection=0;        
        wickremain=24;      wick=0;
        oliremain=40;       oli=0;
        phantremain=32;     phant=0;

        do {

        displayOptions();
        selection=getUserOption("Option? (1-7): ");

        System.out.println();

        if(selection==1){ 


        System.out.println("Wicked (Apollo)");
        if  ( wick>=24){
        System.out.println("Sold Out");
    }   else {      
        System.out.println(wick +" ticket(s) Sold, "+ wickremain+ " ticketAvailable");
    }


        System.out.println("Oliver! (Drury Lane)");
        if  ( oli>=40){
        System.out.println("Sold Out");
    }   else {      
        System.out.println(oli +" ticket(s) Sold, "+ oliremain +" tickets      Available");
    }


        System.out.println("Phantom of the Opera (Her Majesty's)");
        if  ( phant>=32){
        System.out.println("Sold Out");
    }   else {      
        System.out.println(phant +" ticket(s) Sold, " +phantremain+ " tickets Available");
        }
        }
4

6 回答 6

0

根据我对您问题的理解,这就是我将如何解决它:

只需在displayOptions()方法中添加一个检查。

首先,您必须在类中添加一个字段,以了解是否要显示 Finish。

public class part3 {
    private static boolean showFinish = false;

然后if在方法中添加一个子句:

if (showFinish) {
    System.out.println("7. Finish");
} else {
    System.out.println("7. Exit");
}

最后,当您想更改为 Finish 时,将其放在调用之前displayOptions()

showFinish = true;
于 2013-11-14T16:31:41.277 回答
0

不要在 Java 中重新发明提示,使用可用的工具,例如:

http://jline.sourceforge.net/

https://github.com/jline/jline2

用于解析 args

args4j - Java 命令行参数解析器

Commons CLI

控制台上的颜色输出

扬斯

于 2013-11-14T16:33:54.057 回答
0

让它成为一个变量?

String optionSeven = "blah";

然后根据条件更新 optionSeven 其他地方?

于 2013-11-14T16:35:49.877 回答
0

您可以使用选择语句

String exit = "Exit";
String finish = "Finish";
String doh = "DOHH!";

if (someCondition){
    System.out.println("7: " + exit);
} else if (someOtherCondition){
    System.out.println("7: " + finish);
} else {
    System.out.println("7: " + doh);
}
于 2013-11-14T16:35:53.337 回答
0

如果将所有选项保存为字符串数组,则可以要求用户输入他们想要更改的索引位置。所以:

String[] menuOptions = { option1, option2, etc.};
for(int i = 0; i < menuOptions.length - 1; i++)
    System.out.println((i+1) + ". " + menuOptions[i]);

System.out.println("What option would you like to change?");

int temp = k.nextInt();

System.out.println("What would you like that option to become?");

menuOptions[temp-1] = k.nextLine();
于 2013-11-14T16:38:48.053 回答
0

据我了解,您希望能够在运行时动态更改菜单选项的文本。我相信您最好的选择是为您的菜单项使用列表或其他容器,并调用一个方法来更改给定项的文本。例如,您可能有一个名为 options 的成员变量:

private List<String> options = getDefaultOptions();

你可以有一种方法来生成默认选项:

public List<String> getDefaultOptions() {
    List<String> defaultOptions = new ArrayList<>();
    defaultOptions.add("List all shows and availability");
    defaultOptions.add("Book and print ticket(s)");
    defaultOptions.add("Cancel a ticket");
    defaultOptions.add("Display theatre/show seating map");
    defaultOptions.add("Show income/accounts");
    defaultOptions.add("Assign new show to theatre");
    defaultOptions.add("Exit");
    return defaultOptions;
}

您的 displayOptions() 方法将变为:

public void displayOptions() {
    System.out.println("Choose your option:");
    System.out.println("**** West End Theatre Booking System ****");
    System.out.println("=========================================");

    int optionNumber = 0;
    for (String option : options) {
        System.out.println(++optionNumber + ". " + option);
    }
}

然后,在某些时候,您可以提示用户更改特定选项的文本,并使用他们输入的文本调用此方法:

public void changeOptionText(int optionNumber, String newText) {
    options.set(optionNumber-1, newText);
}

...

// Used like this
changeOptionText(7, "finish");

当然,您需要进行一些边界检查并涵盖其他一些情况,但这应该可以帮助您入门。

于 2013-11-14T16:59:04.007 回答