题解HJ17 | #坐标移动#
坐标移动
https://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 的区别 String s = in.nextLine(); String[] stringArr = s.split(";"); int x = 0; int y = 0; for (int i = 0; i < stringArr.length; i++) { //判断非空 if (stringArr[i].isEmpty()) continue; //正则表达式校验 if (!stringArr[i].matches("^[WASD]{1}[0-9]{1,2}$")) continue; //取数字 int num = Integer.valueOf(stringArr[i].substring(1)); //数字仅为正数 if (num < 0) continue; switch (stringArr[i].substring(0, 1)) { case "W": y += num; break; case "A": x -= num; break; case "S": y -= num; break; case "D": x += num; break; } } //if(x<Math.pow((-2),31)||y>Math.pow(2,31)-1) System.out.println(x + "," + y); } }
正则表达式卡了我好久