题解 | #坐标移动#

坐标移动

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);
        String split = ";";
        String str = in.nextLine();
        int key = 0 ;
        int value =0;
        List<Integer> list = new ArrayList<>();
        list.add(key);//初始化坐标
        list.add(value);
        if (str.length()>1 && str.length()<=10000){
            String[] strings = str.split(split);//取坐标数组
            for (String string : strings) {
                //校验格式是否正确
                if (string.matches("^[A-Z][0-9]+(\\d{1})*$")) {
                    String substring = string.substring(1);//截取坐标值
                    int math = Integer.parseInt(substring);
                    switch (string.substring(0, 1)) {
                        case "A":
                            key = list.get(0) - math;
                            list.set(0, key);
                            break;
                        case "D":
                            key = list.get(0) + math;
                            list.set(0, key);
                            break;
                        case "W":
                            value = list.get(1) + math;
                            list.set(1, value);
                            break;
                        case "S":
                            value = list.get(1) - math;
                            list.set(1, value);
                            break;
                    }
                }
            }
            System.out.println(list.get(0)+","+ list.get(1));
        }
    }
}

全部评论

相关推荐

尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务