判断指令有效:首字母是A、D、W、S,且指令中非数字的只有一个,即首字母
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] cmd = in.nextLine().split("\\;");
int posX = 0,posY = 0;
for(String s : cmd){
if(isCmdVaild(s)){
String moveStep = s.substring(1,s.length());
switch(s.charAt(0)){
case 'A':
posX -= Integer.parseInt(moveStep);
break;
case 'D':
posX += Integer.parseInt(moveStep);
break;
case 'W':
posY += Integer.parseInt(moveStep);
break;
case 'S':
posY -= Integer.parseInt(moveStep);
break;
}
}
}
System.out.println(posX + "," + posY);
}
}
public static boolean isCmdVaild(String s){
int count = 0;//统计非数字的个数,大于1则命令无效
if(s.length()==0 || s == ""){//空
return false;
}
if(!(s.charAt(0) == 'A') && !(s.charAt(0) == 'D') && !(s.charAt(0) == 'W') &&
!(s.charAt(0) == 'S')){
return false;
}else{
for(Character ch : s.toCharArray()){
if(!Character.isDigit(ch)){
count++;
}
}
}
return count == 1;//
}
}