java需要帮助创建一个“help”命令,用户可以在任何时候键入该命令,查看关键字,或者在遇到问题时获得帮助
我正在为我的AP计算机科学期末项目创建一个基于文本的RPG,我想这样做,玩家可以在任何时候键入“help”,并在控制台中显示一条消息。我能想到的唯一方法是使用扫描仪导入并执行以下操作:
String help = scan.nextLine();
switch(help) {
case help:
System.out.println("help message");
break;
}
我一直在为这个项目自学Java,我是个新手,我知道这可能是一个非常低效的方法,更不用说它只能在1点上工作。因此,如果有人能给我指出正确的方向,我将永远感激。 另外:在提交这篇文章之前,我已经搜索了答案,但是我找不到一个描述如何在整个游戏过程中将其打印到控制台的答案
# 1 楼答案
如果。。埃利夫。。然后使用语句来捕获命令,而不是switch语句。别忘了用绳子。equals()方法来比较字符串!看看为什么
# 2 楼答案
您可以创建一个处理请求帮助的方法,您将始终使用该方法而不是
scan.nextLine()
:现在,无论在哪里使用
scan.nextLine
,都要使用getNextLine(scan)
。这样,当用户输入“帮助”时,您将自动计算。(只是一个提示,您可能想使用equalsIgnoreCase
而不是equals
,这样,如果用户键入“Help”,您的代码仍然有效。)# 3 楼答案
除了HelperFormatter之外,我还会使用Apache Commons CommandLine类: