题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (sc.hasNextLine()) {
int x = 0;
int y = 0;
String f = sc.nextLine();
String[] arr = f.split(";");
for (int i = 0; i < arr.length; i++) {
if(!arr[i].matches("[WASD][0-9]{1,2}")) {
arr[i]="A00";
}
}
for (int i = 0; i < arr.length; i++) {
if (arr[i].charAt(0) == 'A') {
x -= Integer.parseInt(arr[i].substring(1));
} else if (arr[i].charAt(0) == 'S') {
y -= Integer.parseInt(arr[i].substring(1));
} else if (arr[i].charAt(0) == 'D') {
x += Integer.parseInt(arr[i].substring(1));
} else if (arr[i].charAt(0) == 'W') {
y += Integer.parseInt(arr[i].substring(1));
}
}
System.out.println(x + "," + y);
}
}
}
