题解 | 坐标移动

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    //判断指令是否合法
    private static boolean isValidCommand(String command) {
        command = command.trim();
        boolean flag = false;
        //指令长度为3或4个字符
        if (command == null || command.length() < 2 || command.length() > 3) {
            return flag;
        }
        //第一个符号为 "A/D/W/S" 中的一个
        char first = command.charAt(0);
        if (!isValidFirstChar(first)) {
            return flag;
        }
        char second = command.charAt(1);
        int code = second - '0';
        if (command.length() == 2) {//长度为2时,第二个符号必为数字1-9;
            if (code < 1 || code > 9) {
                return flag;
            }
        } else if (command.length() ==
                   3) {//长度为3时,第二个符号为数字0-9;第三个符号为0-9;

            int thirdCode = command.charAt(2) - '0';
            if (code < 0 || code > 9 || thirdCode < 0 || thirdCode > 9 || (code == 0 &&
                    thirdCode == 0)) {
                return flag;
            }
        }
        flag = true;
        return flag;
    }

    //判断第一个符合是否合法
    private static boolean isValidFirstChar(char c) {
        List<Character> list = Arrays.asList('A', 'S', 'D', 'W');
        if (!list.contains(c)) {
            return false;
        }
        return true;
    }

    //定义坐标点
    static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    /**
     * @description 移动
     * @param point 坐标点
     * @param direction 方向
     * @param distance 距离
     * @return 坐标点
     */
    public static Point move(Point point, char direction, int distance) {
        switch (direction) {
            case 'A':
                point.x -= distance;
                break;
            case 'D':
                point.x += distance;
                break;
            case 'W':
                point.y += distance;
                break;
            case 'S':
                point.y -= distance;
                break;
        }
        return point;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String command = scanner.nextLine().trim();
            if ("exit".equalsIgnoreCase(command)) {
                break;
            }
            String[] arrs = command.split(";");
            Point point = new Point(0, 0);
            for (String str : arrs) {
                if (isValidCommand(str)) {//判断指令是否合法
                    char direction = str.charAt(0);
                    int distance = 0;
                    if (str.length() == 2) {
                        distance = str.charAt(1) - '0';
                    } else if (str.length() == 3 && (str.charAt(1) - '0' > 0)) {
                        distance = (str.charAt(1) - '0') * 10 + (str.charAt(2) - '0');
                    } else if (str.length() == 3 && (str.charAt(1) - '0' == 0)) {
                        distance = str.charAt(2) - '0';
                    }
                    point = move(point, direction, distance);
                }
            }
            System.out.println(point.x + "," + point.y);
        }
        scanner.close();
    }
}

全部评论

相关推荐

最喜欢秋天的火龙果很...:第一份工作一定要往大的去,工资低点没事。后面换工作会更好找,即使你去小公司,你也不可能不会换工作的。所以找大的去
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务