题解 | #坐标移动#
坐标移动
http://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);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) {
System.out.println(myAns(in.next()));
}
}
private static String myAns(String str) {
int x = 0, y = 0;
String[] strs = str.split(";");
for(String s : strs) {
if( s == null || s.trim().length() == 0) {
continue;
}
String di = s.substring(0,1);
if(!("A".equals(di) || "D".equals(di) || "W".equals(di) || "S".equals(di)) ){
continue;
}
int mv = 0;
try{
mv = Integer.valueOf(s.substring(1));
}catch(Exception e){
continue;
}
if("A".equals(di)){
x -= mv;
} else if("D".equals(di)){
x += mv;
} else if("W".equals(di)){
y += mv;
} else if("S".equals(di)){
y -= mv;
}
}
return x + "," + y;
}
}