题解 | #坐标移动#
坐标移动
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);
// 注意 hasNext 和 hasNextLine 的区别
int x = 0;
int y = 0;
String command = in.nextLine();
String[] comds = command.split(";");
for(String comd : comds){
if(comd.length()==1){
continue;
}
if(comd == null || "".equals(comd)){
continue;
}
char direction = comd.charAt(0);
boolean flag = true;
for( int i=1;i<comd.length();i++){
if('0' > comd.charAt(i) || comd.charAt(i) > '9'){
flag = false;
}
}
if(!flag){
continue;
}
switch(direction) {
case 'A' :
x-= Integer.valueOf(comd.substring(1));
break;
case 'D' :
x+= Integer.valueOf(comd.substring(1));
break;
case 'W' :
y+= Integer.valueOf(comd.substring(1));
break;
case 'S' :
y-= Integer.valueOf(comd.substring(1));
}
}
System.out.println(x+","+y);
}
}
查看15道真题和解析