题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] arr = {0,0};
String pattern = "[WASD][0-9]{1,2}";
while(sc.hasNext()){
String str = sc.nextLine();
String[] s = str.split(";");
for(int i = 0; i < s.length ; i++){
if(s[i].matches(pattern)){
arr = Coord(s[i],arr);
}
}
System.out.println(arr[0]+","+arr[1]);
}
}
static int[] Coord(String coord,int[] array){
int[] arr = array;
int x = arr[0];
int y = arr[1];
int num = Integer.parseInt(coord.substring(1));
switch(coord.charAt(0)){
case 'W':
y += num;
break;
case 'A':
x -= num;
break;
case 'S':
y -= num;
break;
case 'D':
x += num;
break;
}
arr[0] = x;
arr[1] = y;
return arr;
}
}