如何检查 userInput 命令中的特定部分?
How do I check for specific parts in a userInput command?
我正在尝试制作游戏.. 当有人在控制台中键入命令时,我希望命令中包含 [args]。
例如:
~治愈[用户名][健康]
这里是一个例子代码(如果你有答案请参考这个):
import java.util.Scanner;
class WhosebugExample {
public static void main(String[] args) {
System.out.println("Healing Command: ~heal [username] [health]");
int player1HP = 0;
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
if (userInput.equals("~heal " /**+ username + num**/) /**How do I make it so that a number(the amount to heal) and a username(player username) can be inputted after?**/){
player1HP += 0; /**I need the number that the user inputs to be added to player1HP**/
}
System.out.println("player1/**I want this to be the username value**/ is at: " + player1HP/**I want this to be the hp value + player1HP**/ + " hp.");//I want this command to print out "ProGamer is at: 3 hp."
}
}
输出:
如何获取“用户名”和“健康”的值?
拆分。
userInput.split(" ")
您可以使用regular expression来确保输入有效。
然后您可以调用方法 split 将命令分成单独的单词。
import java.util.Scanner;
public class WhosebugExample {
public static void main(String[] args) {
System.out.println("Healing Command: ~heal [username] [health]");
int player1HP = 0;
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
if (userInput.matches("^~heal [^ ]+ \d+$")) {
String[] parts = userInput.split(" ");
int health = Integer.parseInt(parts[2]);
player1HP += health;
String player = parts[1];
System.out.printf("%s is at: %d hp.%n", player, player1HP);
}
}
}
正则表达式检查输入的命令是否以 ~heal
开头,后跟一个 space 后跟一个或多个不是 space 的字符,后跟另一个 [=24] =] 后跟一位或多位数字。
然后输入的命令在 space 上拆分,其中 returns 一个三元素数组,其中第一个数组元素是命令(即 ~heal
),第二个元素是玩家名称,最后一个元素是生命值。
我正在尝试制作游戏.. 当有人在控制台中键入命令时,我希望命令中包含 [args]。
例如:
~治愈[用户名][健康]
这里是一个例子代码(如果你有答案请参考这个):
import java.util.Scanner;
class WhosebugExample {
public static void main(String[] args) {
System.out.println("Healing Command: ~heal [username] [health]");
int player1HP = 0;
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
if (userInput.equals("~heal " /**+ username + num**/) /**How do I make it so that a number(the amount to heal) and a username(player username) can be inputted after?**/){
player1HP += 0; /**I need the number that the user inputs to be added to player1HP**/
}
System.out.println("player1/**I want this to be the username value**/ is at: " + player1HP/**I want this to be the hp value + player1HP**/ + " hp.");//I want this command to print out "ProGamer is at: 3 hp."
}
}
输出:
如何获取“用户名”和“健康”的值?
拆分。
userInput.split(" ")
您可以使用regular expression来确保输入有效。
然后您可以调用方法 split 将命令分成单独的单词。
import java.util.Scanner;
public class WhosebugExample {
public static void main(String[] args) {
System.out.println("Healing Command: ~heal [username] [health]");
int player1HP = 0;
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
if (userInput.matches("^~heal [^ ]+ \d+$")) {
String[] parts = userInput.split(" ");
int health = Integer.parseInt(parts[2]);
player1HP += health;
String player = parts[1];
System.out.printf("%s is at: %d hp.%n", player, player1HP);
}
}
}
正则表达式检查输入的命令是否以 ~heal
开头,后跟一个 space 后跟一个或多个不是 space 的字符,后跟另一个 [=24] =] 后跟一位或多位数字。
然后输入的命令在 space 上拆分,其中 returns 一个三元素数组,其中第一个数组元素是命令(即 ~heal
),第二个元素是玩家名称,最后一个元素是生命值。