题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String content = scanner.nextLine();
int[][] res = {{0, 0}};
String[] contents = content.split(";");
for (String c : contents) {
if (c.startsWith("A") && c.length() != 1) {
c = c.substring(1);
if (isNum(c)) {
res[0][0] -= Integer.parseInt(c);
}
}
if (c.startsWith("D") && c.length() != 1) {
c = c.substring(1);
if (isNum(c)) {
res[0][0] += Integer.parseInt(c);
}
}
if (c.startsWith("W") && c.length() != 1) {
c = c.substring(1);
if (isNum(c)) {
res[0][1] += Integer.parseInt(c);
}
}
if (c.startsWith("S") && c.length() != 1) {
c = c.substring(1);
if (isNum(c)) {
res[0][1] -= Integer.parseInt(c);
}
}
}
System.out.println(res[0][0] + "," + res[0][1]);
}
public static boolean isNum(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
