题解 | #坐标移动#
坐标移动
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); int x = 0; int y = 0; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String line = in.nextLine(); String spl[] = line.split(";"); for(String str:spl){ if (str.startsWith("A") || str.startsWith("S") || str.startsWith("W") || str.startsWith("D")) { String move = ""; // 是否中断 boolean finish = false; for(int i=1;i<str.length();i++){ char c = str.charAt(i); if (c >= '0' && c<='9'){ move += String.valueOf(c); } else { finish = true; break; } } if (!finish){ char c = str.charAt(0); int speed = Integer.parseInt(move); switch(c){ case 'A': x -= speed; break; case 'S': y -= speed; break; case 'W': y += speed; break; case 'D': x += speed; break; } } } } System.out.println(x +","+y); } } }