题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("A", "D", "W", "S");
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            Integer a = 0;
            Integer b = 0;
            coo(list, in.nextLine(), a, b);
        }
    }

    private static void coo(List<String> list, String coo, Integer a, Integer b) {
        if (null == coo || coo.length() > 10000) {
            return;
        }

        String[] split = coo.split(";");
        for (String v : split) {
            if (null != v && v.length() > 0) {
                String direction = v.substring(0, 1);
                boolean contains = list.contains(direction);
                int value;
                String valueStr = v.substring(1);
                boolean matches = valueStr.matches("^\\d+$");
                if (contains && matches) {
                    value = Integer.parseInt(valueStr);
                    if (value < 1 || value > 99) {
                        continue;
                    }
                    // 开始计算
                    switch (direction) {
                        case "A":
                            a = a - value;
                            break;
                        case "D":
                            a = a + value;
                            break;
                        case "W":
                            b = b + value;

                            break;
                        case "S":
                            b = b - value;
                            break;
                        default:
                    }
                }
            }
        }
        System.out.println( a + "," + b );
    }
}

全部评论

相关推荐

10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
12-03 15:41
已编辑
淘天 算法工程师 31.0k*16.0
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务