如何添加搜索方法作为命令输入
How to add a search method as command input
我正在做一个程序,可以添加参与者、参与者的结果和体育比赛的事件。我希望能够通过将事件名称写为命令来打印出每个事件的所有结果。然后程序应该在我的 arrayList
中搜索事件并查看它是否存在,如果存在则打印出结果。
我有一种在 arraylist
中搜索事件的方法,但我无法将方法用作命令。我知道使用 event result
之类的通用名称然后在其中添加方法会更容易,但要求您通过命令搜索列表中的事件。
boolean running = true;
while(running) {
System.out.print("Command> ");
String cmd = readString().toLowerCase();
if (cmd.equals("message")) printMessage();
else if (cmd.equals("add participant")) addParticipant();
else if (cmd.equals("check participant")) listParticipant();
else if (cmd.equals("remove participant")) removeParticipant();
else if (cmd.equals("add result")) addResult();
else if (cmd.equals("participant")) listParticipantResult();
-> //else if (cmd.equals(findEvent()) listEvent();
else if (cmd.equals("end")) {
System.out.println("Exit!");
running = false;
} else System.out.println("Wrong command!");
}
如您所说,如果您想搜索事件名称并且希望命令看起来像这样:"find yourEventName",那么您需要在最后一个 else if
语句中做的就是检查命令是否为 "find yourEventName",然后拆分该字符串并仅获取 "yourEventName"。当您拥有它时,只需使用事件名称作为参数调用您的方法 findEvent()
,如果找到它,就可以在该方法中对该事件执行任何您想要的操作。像这样:
else if (cmd.equals("find yourEventName")) {
String eventName = cmd.split(" ")[1];
findEvent(eventName);
}
希望对您有所帮助。
假设您的命令将直接是事件名称。前任。 SportTornament
// Above all if else
//assuming cmd will be directly your event name. search for event name in eventList if it exists then call findEvent method with eventName( here it will be cmd) as parameter.
} else if (eventList.contains(cmd)) {
findEvent(cmd);
}
假设您的命令会找到
//Above all if else
//split the input as find saparate and other remaining string as eventName
} else if (cmd.startsWith("find")) {
String eventName = cmd.substring(4, cmd.length());
findEvent(eventName);
}
我正在做一个程序,可以添加参与者、参与者的结果和体育比赛的事件。我希望能够通过将事件名称写为命令来打印出每个事件的所有结果。然后程序应该在我的 arrayList
中搜索事件并查看它是否存在,如果存在则打印出结果。
我有一种在 arraylist
中搜索事件的方法,但我无法将方法用作命令。我知道使用 event result
之类的通用名称然后在其中添加方法会更容易,但要求您通过命令搜索列表中的事件。
boolean running = true;
while(running) {
System.out.print("Command> ");
String cmd = readString().toLowerCase();
if (cmd.equals("message")) printMessage();
else if (cmd.equals("add participant")) addParticipant();
else if (cmd.equals("check participant")) listParticipant();
else if (cmd.equals("remove participant")) removeParticipant();
else if (cmd.equals("add result")) addResult();
else if (cmd.equals("participant")) listParticipantResult();
-> //else if (cmd.equals(findEvent()) listEvent();
else if (cmd.equals("end")) {
System.out.println("Exit!");
running = false;
} else System.out.println("Wrong command!");
}
如您所说,如果您想搜索事件名称并且希望命令看起来像这样:"find yourEventName",那么您需要在最后一个 else if
语句中做的就是检查命令是否为 "find yourEventName",然后拆分该字符串并仅获取 "yourEventName"。当您拥有它时,只需使用事件名称作为参数调用您的方法 findEvent()
,如果找到它,就可以在该方法中对该事件执行任何您想要的操作。像这样:
else if (cmd.equals("find yourEventName")) {
String eventName = cmd.split(" ")[1];
findEvent(eventName);
}
希望对您有所帮助。
假设您的命令将直接是事件名称。前任。 SportTornament
// Above all if else
//assuming cmd will be directly your event name. search for event name in eventList if it exists then call findEvent method with eventName( here it will be cmd) as parameter.
} else if (eventList.contains(cmd)) {
findEvent(cmd);
}
假设您的命令会找到
//Above all if else
//split the input as find saparate and other remaining string as eventName
} else if (cmd.startsWith("find")) {
String eventName = cmd.substring(4, cmd.length());
findEvent(eventName);
}