题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.next();
String[] strings = str.split(";");
int x = 0;
int y = 0;
for (int i =0; i<strings.length; i++) {
if (strings[i].length() < 2 || strings[i].length() >3) continue; //一位数、空字符或大于三位
char dir = strings[i].charAt(0);
//判断剩下的字符是否全是数字
boolean isDig = true;
for (int j = 1; j < strings[i].length(); j++) {
if (!Character.isDigit(strings[i].charAt(j))) {
isDig = false;
}
}
if (!isDig) continue;
int nums = Integer.parseInt(strings[i].substring(1));
if (dir == 'A') { //左
x = x - nums;
} else if (dir == 'D') {
x = x + nums;
} else if (dir == 'W') {
y = y + nums;
} else if (dir == 'S') {
y = y - nums;
}
}
System.out.print(x + "," + y);
}
}
