【发布时间】:2021-08-13 06:22:23
【问题描述】:
我正在制作一个主机游戏.. 我希望能够同时检查 2 个人的信息。就我而言,我想做一个“kill checker”命令。命令是
~killc [username]
使用此命令,我将能够检查 1 人的杀戮情况。如果我想检查 2 个人怎么办?我将如何使用我的 .matches(regex) 来计算 2 个字符串?我试过打字:
"^~killc [^ ] [^ ]+$", and "^~killc [^ ] + [a-zA-Z]+$"
但它们不起作用。 请阅读下面的代码以获取更多信息。
import java.util.Scanner;
class StackOverflowExample {
static int kills = 10;
public static void main(String[] args) {
System.out.println("Kill count command: ~killc [username]");
Scanner userInt = new Scanner(System.in);
String userInput = userInt.nextLine();
if (userInput.matches("^~killc [^ ]+$"/**How would I input more than one username?**/)){
String[] parts = userInput.split(" ");
String username = parts[1];
System.out.printf("%s has " + kills + " kills.",username);
}
}
}
【问题讨论】:
-
这个“不起作用”到底是怎么回事?您是否收到错误消息、意外输出或比“不起作用”更具体的内容?
-
@NickReed 没有任何反应。我播放代码,它就结束了。
标签: java regex console command java.util.scanner