想知道是否有人可以帮我查询一下?无论如何在java中为我创建一个promt给用户,允许他们更改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");
}
}
答案 0 :(得分:0)
根据我如何理解你的问题,我就是这样解决的:
只需在displayOptions()
方法中添加一项检查。
首先,您必须在课程中添加一个字段,以了解是否要显示“完成”。
public class part3 {
private static boolean showFinish = false;
然后在方法中添加if
子句:
if (showFinish) {
System.out.println("7. Finish");
} else {
System.out.println("7. Exit");
}
最后,如果要更改为“完成”,请在调用displayOptions()
:
showFinish = true;
答案 1 :(得分:0)
不要在Java中重新发明提示,使用可用的工具,例如:
https://github.com/jline/jline2
用于解析args
args4j - Java command line arguments parser
控制台上的颜色输出
答案 2 :(得分:0)
将它变成一个变量?
String optionSeven = "blah";
然后根据条件更新optionSeven else?
答案 3 :(得分: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);
}
答案 4 :(得分: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();
答案 5 :(得分: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");
当然,你想要做一些界限检查并覆盖其他一些案例,但这应该让你开始。