题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
String[] array = input.split(";");
int x = 0;
int y = 0;
for (String str : array) {
// 去除空字符串
if (str.trim().equals("")) {
continue;
}
// 获取移动方向
String way = String.valueOf(str.charAt(0));
// 移动距离
int size = 0;
try{
size = Integer.parseInt(str.substring(1));
} catch (NumberFormatException e) {
continue;
}
switch(way) {
case "A":
x -= size;
break;
case "D":
x += size;
break;
case "W":
y += size;
break;
case "S":
y -= size;
break;
default:
break;
}
}
System.out.println(x + "," + y);
}
}