0

好的,所以我想做的是,在这个名为 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?
            }
4

2 回答 2

2

你可以使用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
    }
}
于 2013-10-30T03:16:16.403 回答
1

您可以使用正则表达式来提取数值。

Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
Matcher m = p.matcher(time);

if (m.find()) {
   System.out.println(m.group(1));
}
于 2013-10-30T03:24:42.067 回答