题解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);
    }
}

正则表达式卡了我好久

全部评论

相关推荐

03-07 13:49
门头沟学院 Java
逆流河上万仙退:可能是发的钱太少了 怕你过来实习还要自己贴钱 意向就不高 省的浪费大家时间 可能你通过了也不会去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务