题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); String[]arr = str.split(";"); int x = 0, y = 0; for (String s : arr) { if (s.length() < 2) { continue; } if (s.startsWith("A")) { int a = getInt(s.substring(1)); if (a != -1) { x -= a; } } else if (s.startsWith("D")) { int d = getInt(s.substring(1)); if (d != -1) { x += d; } } else if (s.startsWith("W")) { int w = getInt(s.substring(1)); if (w != -1) { y += w; } } else if (s.startsWith("S")) { int a = getInt(s.substring(1)); if (a != -1) { y -= a; } } } System.out.println(x + "," + y); } public static int getInt(String str) { int i = -1; try { i = Integer.parseInt(str); } catch (Exception e) {} finally {} return i; } }