题解 | #坐标移动#

坐标移动

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);
        List list = new ArrayList<String>();
        try {
            String a = in.nextLine();
            String[] str = a.split(";");
            for (int i = 0; i < str.length; i++) {
                if (str[i].matches("[A-Z][0-9][0-9]")) {
                    list.add(str[i]);
                }
            }
            int x = 0, y = 0 ; //坐标
            for (String st : str) {
                if (st.length() < 2  || !(st.substring(1).matches("[0-9][0-9]") ||
                                          st.substring(1).matches("[0-9]")) ) {
                    continue;
                }
                String leap = st.substring(0, 1);
                int n = Integer.valueOf(st.substring(1));
                switch (leap) {
                    //向左移动
                    case "A":
                        x = x - n;
                        break;
                    //向右移动
                    case "D":
                        x = x + n;
                        break;
                    //向上移动
                    case "W":
                        y = y + n;
                        break;
                    //向下移动
                    case "S":
                        y = y - n;
                        break;
                }
            }

            System.out.print(x + "," + y);
        } catch (InputMismatchException e) {
            System.out.print("你的输入有误!");
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 13:47
机械打工仔:你自己匿名可以,这么好的公司就别给它匿名了
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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