题解 | #坐标移动#

坐标移动

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();
            }
        }
    }
}

全部评论

相关推荐

迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
10-29 15:51
嘉应学院 Java
后端转测开第一人:你把简历的学历改成北京交通大学 去海投1000份发现基本还是没面试
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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