题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
String[] arr = line.split(";");
// 标记x,y
int x = 0, y = 0;
for (int i = 0; i < arr.length; i++) {
String str = arr[i];
if (Objects.equals(str, "") || str == null) continue;
char op = str.charAt(0);
String step = str.substring(1);
// 判断指令是否合法
if (op != 'A' && op != 'D' && op != 'W' && op != 'S' ||
!isNumeric(step)) continue;
int stepCount = Integer.parseInt(step);
switch (op) {
case 'A':
x -= stepCount;
break;
case 'D':
x += stepCount;
break;
case 'W':
y += stepCount;
break;
case 'S':
y -= stepCount;
break;
}
}
System.out.println(x + "," + y);
}
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
}