题解 | #坐标移动#

坐标移动

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

全部评论

相关推荐

05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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