使用的方法比较粗糙,先利用字符串分割,再使用正则匹配,将结果循环字符串处理,分为上下两轴。
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String line = s.nextLine();
String[] split = line.split("\\W");
List<String> strings = Arrays.asList(split);
List<String> resultList = strings.stream().filter(str -> str.matches("^[A|W|S|D][0-9]+$")).collect(Collectors.toList());
int l =0;
int r = 0;
for (String move : resultList) {
if (move.charAt(0) == 'A'){
l -= Integer.parseInt(move.substring(1));
}
if (move.charAt(0) == 'D'){
l += Integer.parseInt(move.substring(1));
}
if (move.charAt(0) == 'S'){
r -= Integer.parseInt(move.substring(1));
}
if (move.charAt(0) == 'W'){
r += Integer.parseInt(move.substring(1));
}
}
System.out.println(l+","+r);
}
}