题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] arr = in.nextLine().split(";"); int xVal = 0; int yVal = 0; // System.out.println(arr.length); for(int i = 0; i < arr.length; i++) { //合法坐标为A(或者D或者W或者S) + 数字(两位以内) //{n,m} Matches n to m repetitions of the previous character or expression. if(!arr[i].matches("[ASWD][0-9]{1,2}")) { continue; } int val = Integer.valueOf(arr[i].substring(1)); // take the substring from 1 if(arr[i].charAt(0) == 'A') { xVal -= val; } else if(arr[i].charAt(0) == 'D') { xVal += val; } else if(arr[i].charAt(0) == 'S') { yVal -= val; } else if(arr[i].charAt(0) == 'W') { yVal += val; } } System.out.println(xVal+","+yVal); } }