您可以使用字符串拆分创建交互式菜单吗?
Can you create an interactive menu using a string split?
对于学校的一个项目,我的教授希望我们使用字符串拆分创建一个菜单。她没有介绍如何做到这一点,所以我有点迷茫。
这里是赋值说明:
“对于项目 4,我希望你使用项目 3 并在其上使用基于命令行的菜单。这意味着你将使用命令字符串版本而不是交互式菜单,即
买1
历史
退出
您的程序必须表明您可以处理实现程序的两种类型的接口。一种使用交互式菜单询问他们想做什么,另一种使用命令字符串菜单,您在其中输入命令作为字符串,您的程序使用字符串拆分。"
我似乎找不到显示如何根据字符串用户输入制作菜单的任何其他资源。我不知道从哪里开始,我将不胜感激任何帮助。谢谢!
System.out.println("Welcome! Enter a command. \n"
+ "Enter 1) Buy Bitcoin \n"
+ "Enter 2) Sell Bitcoin \n"
+ "Enter 3) Print Balance \n"
+ "Enter 4) Print History \n"
+ "ENTER 5) print USD\n"
+ "Enter 6) Exit Program\n");
choice = myscanner.nextInt();
正如威斯勒在评论中所述,您应该阅读整行并拆分字符串。以下代码示例向您展示了解决此任务的方法:
// Example command
String command = "print balance";
String[] commandSplitted = command.split(" ");
// You have to add a length check for the array
if (commandSplitted[0].equals("print")) {
// command part 1 is print
if (commandSplitted[1].equals("history")) {
// command part 2 is history
// command is print history
// ...
System.out.println("enter 4");
} else if (commandSplitted[1].equals("balance")) {
// command part 2 is balance
// command is print balance
// ...
System.out.println("enter 3");
}//... depending on the amount of commands you might want to use a switch case on the split parts
} // else if ...
您的下一步是完成此代码并添加上一个任务中的 reader(您必须对其进行修改)。
请注意,这只是一种方法,并非完全有效。
对于学校的一个项目,我的教授希望我们使用字符串拆分创建一个菜单。她没有介绍如何做到这一点,所以我有点迷茫。
这里是赋值说明:
“对于项目 4,我希望你使用项目 3 并在其上使用基于命令行的菜单。这意味着你将使用命令字符串版本而不是交互式菜单,即
买1 历史 退出
您的程序必须表明您可以处理实现程序的两种类型的接口。一种使用交互式菜单询问他们想做什么,另一种使用命令字符串菜单,您在其中输入命令作为字符串,您的程序使用字符串拆分。"
我似乎找不到显示如何根据字符串用户输入制作菜单的任何其他资源。我不知道从哪里开始,我将不胜感激任何帮助。谢谢!
System.out.println("Welcome! Enter a command. \n"
+ "Enter 1) Buy Bitcoin \n"
+ "Enter 2) Sell Bitcoin \n"
+ "Enter 3) Print Balance \n"
+ "Enter 4) Print History \n"
+ "ENTER 5) print USD\n"
+ "Enter 6) Exit Program\n");
choice = myscanner.nextInt();
正如威斯勒在评论中所述,您应该阅读整行并拆分字符串。以下代码示例向您展示了解决此任务的方法:
// Example command
String command = "print balance";
String[] commandSplitted = command.split(" ");
// You have to add a length check for the array
if (commandSplitted[0].equals("print")) {
// command part 1 is print
if (commandSplitted[1].equals("history")) {
// command part 2 is history
// command is print history
// ...
System.out.println("enter 4");
} else if (commandSplitted[1].equals("balance")) {
// command part 2 is balance
// command is print balance
// ...
System.out.println("enter 3");
}//... depending on the amount of commands you might want to use a switch case on the split parts
} // else if ...
您的下一步是完成此代码并添加上一个任务中的 reader(您必须对其进行修改)。
请注意,这只是一种方法,并非完全有效。