题解 | #坐标移动#

坐标移动

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {

        int[] start = new int[]{0, 0};

        HashMap<Character, Integer[]> actions = new HashMap<>();
        actions.put('D', new Integer[]{1, 0});
        actions.put('A', new Integer[]{-1, 0});
        actions.put('W', new Integer[]{0, 1});
        actions.put('S', new Integer[]{0, -1});
        Scanner in = new Scanner(System.in);
        String positions = in.nextLine();
        String[] positionArray = positions.split(";");
        for (String s : positionArray) {
            try {
                Integer[] integers = actions.get(s.charAt(0));
                int parseInt = Integer.parseInt(s.substring(1));
                start[0] += integers[0] * parseInt;
                start[1] += integers[1] * parseInt;
            } catch (Exception ignored) {
            }
        }
        System.out.println(Arrays.toString(start).replace("[", "").replace("]", "").replace(" ",""));
    }
}

全部评论

相关推荐

Lorn的意义:你这标个前端是想找全栈吗?而且项目确实没什么含金量,技术栈太少了,边沉淀边找吧 现在学院本想就业好一点四年至少得高三模式两年加油吧
点赞 评论 收藏
分享
今天 16:15
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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