题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static boolean check(String str) { if (!str.matches("[WASD][0-9]{1,2}")) { return false; } return true; } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String cmd = in.nextLine(); String[] str = cmd.split(";"); int x = 0, y = 0; for (String s : str) { if (check(s)) { int val = Integer.valueOf(s.substring(1)); switch (s.charAt(0)) { case 'W': y += val; break; case 'S': y -= val; break; case 'A': x -= val; break; case 'D': x += val; break; } } } System.out.print(x + "," + y); } }