题解 | 坐标移动
坐标移动
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 str = in.next();
int x = 0;
int y = 0;
String[] split = str.split(";");
int len = split.length;
for (int i = 0; i < len; i++) {
if (split[i].length() != 3 && split[i].length() != 2) {
continue;
}
int size = split[i].length();
if (size == 3) {
if (split[i].charAt(0) != 'A' && split[i].charAt(0) != 'D' &&
split[i].charAt(0) != 'W' && split[i].charAt(0) != 'S') {
continue;
}
if (split[i].charAt(1) < '0' || split[i].charAt(1) > '9') {
continue;
}
if (split[i].charAt(2) < '0' || split[i].charAt(2) > '9') {
continue;
}
} else {
if (split[i].charAt(0) != 'A' && split[i].charAt(0) != 'D' &&
split[i].charAt(0) != 'W' && split[i].charAt(0) != 'S') {
continue;
}
if (split[i].charAt(1) < '0' || split[i].charAt(1) > '9') {
continue;
}
}
String substring = split[i].substring(1, size);
Integer distance = Integer.valueOf(substring);
if (split[i].charAt(0) == 'A') {
x -= distance;
} else if (split[i].charAt(0) == 'D') {
x += distance;
} else if (split[i].charAt(0) == 'W') {
y += distance;
} else {
y -= distance;
}
}
System.out.print(x);
System.out.print(',');
System.out.print(y);
}
}