0

さて、私がやろうとしているのは、Minecraft というこのゲームで、15h と入力すると、15 時間または 20 分 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 に答える