好吧,我想做的是,在这款名为 Minecraft 的游戏中,如果他们输入 15h,则意味着 15 小时,或 20m,20 分钟。这就是我的想法。
String time = args[3];//args[3] is the text they write (15m, 1d, 20h)
time = time.replace("m", " minutes.");
time = time.replace("h", " hours.");
time = time.replace("d", " days.");
if(time.contains("m"))
{
//Convert the minutes into seconds
//In order to do that I have to pull out the number from "15m", so I would have to pull out 15, how would I do that?
}
最佳答案
您可以使用java.util.Scanner
类。
Scanner s = new Scanner(args[3]);
while (s.hasNextInt()) {
int amount = s.nextInt();
String unit = s.next();
if ("m".equals(unit)) {
// handle minutes
} else if ("h".equals(unit)) {
// handle hours
} else if ("d".equals(unit)) {
// handle days
} else {
// handle unexpected input
}
}
关于java - 从字符串中获取数字 (15m),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673177/