我试图使用Switch语句作为菜单界面,我想知道如何包含一个"帮助"选项,由用户输入'?'触发 符号.
但由于Switch正在接受'char'类型,我不确定这是怎么回事.
你能指点我正确的方向吗?
这是我到目前为止的非编译代码:
private char readChoice()
{ System.out.print("Choice (a/b/c/s/?/x): ");
return In.nextLine().toLowerCase().charAt(0); }
private void execute(char choice)
{ switch (choice)
{ case 'a': routes.show(); break;
case 'b': customers.book(routes); break;
case 'c': customers.show(); break;
case 's': routes.showSchedule(); break;
case '\?': showHelp(); break;
case 'x': break; }}
private String showHelp()
{ String helpText = " A/a Show bookings by route\n";
helpText += " B/b Book a trip\n";
helpText += " C/c Show bookings by customer\n";
helpText += " ? Show choices\n";
helpText += " X/x Exit\n";
return helpText; }
另一个问题是,是否有更合适的方法来实现'退出'选项,而不是仅在输入'x'后休息?
感谢您抽出宝贵时间阅读我的问题.