题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String sinput = in.next(); String[] arr = sinput.split(";"); int x = 0, y = 0; for (String s : arr) { //字符集。匹配包含的任一字符。例如,"[abc]"匹配"plain"中的"a"。 //n 是非负整数。正好匹配 n 次 //数字字符匹配。等效于 [0-9]。 String reg = "[WSAD]{1}\\d{1,2}"; if (null == s || s.length() == 0) continue; if (s.matches(reg)) { String fangxiang = s.substring(0, 1); int num = Integer.valueOf(s.substring(1, s.length())); switch (fangxiang) { case "W": y += num; break; case "S": y -= num; break; case "A": x -= num; break; case "D": x += num; break; } } } System.out.println(x + "," + y); } }