题解 | #坐标移动#

坐标移动

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 str = in.nextLine();
        String[]arr = str.split(";");
        int x = 0, y = 0;
        for (String s : arr) {
            if (s.length() < 2) {
                continue;
            }
            if (s.startsWith("A")) {
                int a = getInt(s.substring(1));
                if (a != -1) {
                    x -= a;
                }
            } else if (s.startsWith("D")) {
                int d = getInt(s.substring(1));
                if (d != -1) {
                    x += d;
                }
            } else if (s.startsWith("W")) {
                int w = getInt(s.substring(1));
                if (w != -1) {
                    y += w;
                }
            } else if (s.startsWith("S")) {
                int a = getInt(s.substring(1));
                if (a != -1) {
                    y -= a;
                }
            }
        }
        System.out.println(x + "," + y);
    }
    public static int getInt(String str) {
        int i = -1;
        try {
            i = Integer.parseInt(str);
        } catch (Exception e) {} finally {}
        return i;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 15:36
点赞 评论 收藏
分享
06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
码农索隆:这种hr,建议全中国推广
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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