【发布时间】:2014-11-07 04:51:17
【问题描述】:
这是我的代码:
public static void rightSel(Scanner scanner,char t)
{
/*if (!stopping)*/System.out.print(": ");
if (scanner.hasNextLine())
{
String orInput = scanner.nextLine;
if (orInput.equalsIgnoreCase("help")
{
System.out.println("The following commands are available:");
System.out.println(" 'help' : displays this menu");
System.out.println(" 'stop' : stops the program");
System.out.println(" 'topleft' : makes right triangle alligned left and to the top");
System.out.println(" 'topright' : makes right triangle alligned right and to the top");
System.out.println(" 'botright' : makes right triangle alligned right and to the bottom");
System.out.println(" 'botleft' : makes right triangle alligned left and to the bottom");
System.out.println("To continue, enter one of the above commands.");
}//help menu
else if (orInput.equalsIgnoreCase("stop")
{
System.out.println("Stopping the program...");
stopping = true;
}//stop command
else
{
String rawInput = orInput;
String cutInput = rawInput.trim();
if (
我想允许用户在如何输入命令方面有一些余地,例如:右上角、右上角、TOPRIGHT、左上角等。
为此,我试图在最后if (,检查cutInput 是否以“top”或“up”开头,并检查cutInput 是否以“left”或“right”结尾,所有同时不区分大小写。这有可能吗?
这样做的最终目标是允许用户在一行输入中从三角形的四个方向之一中进行选择。这是我能想到的最好的方法,但我对一般的编程还是很陌生,可能会使事情变得过于复杂。如果我是,而且有更简单的方法,请告诉我。
【问题讨论】:
-
使用
command.toLowerCase().startsWith("up")和command.toLowerCase().endsWith("right")。
标签: java string case-insensitive startswith ends-with