题解 | #坐标移动#

坐标移动

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

全部评论

相关推荐

11-14 16:13
已编辑
重庆科技大学 测试工程师
Amazarashi66:不进帖子我都知道🐮❤️网什么含金量
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务