题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()){ String[] inStr = sc.nextLine().split(";"); int x = 0; int y = 0; Pattern pattern = Pattern.compile("[0-9]+"); for (String s : inStr) { if(s.length() == 0){ continue; } String head = s.substring(0,1); String tail = s.substring(1); Matcher matcher = pattern.matcher(tail); if(!matcher.matches()){ continue; } if("W".equals(head)){ y += Integer.parseInt(tail); }else if("S".equals(head)){ y -= Integer.parseInt(tail); }else if("A".equals(head)){ x -= Integer.parseInt(tail); }else if("D".equals(head)){ x += Integer.parseInt(tail); } } System.out.println(x + "," + y); } } }