题解 | #坐标移动#

坐标移动

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[] strs = str.split(";");
        int x = 0;
        int y = 0;
        for (int i = 0; i < strs.length; i++) {
            String m = strs[i];
            if (m.length() > 3 || m.length() <= 1) {
                continue;
            }
            int num = 0;
            if (m.length() == 2) {
                if (Character.isDigit(m.charAt(1))) {
                    num = Integer.parseInt(m.substring(1));
                } else {
                    continue;
                }

            }
            if (m.length() == 3) {
                if (Character.isDigit(m.charAt(1)) && Character.isDigit(m.charAt(2))) {
                    num = Integer.parseInt(m.substring(1, 3));
                } else {
                    continue;
                }

            }
            if (m.charAt(0) == ('A')) {
                x -= num;
            }
            if (m.charAt(0) == ('S')) {
                y -= num;
            }
            if (m.charAt(0) == ('W')) {
                y += num;
            }
            if (m.charAt(0) == ('D')) {
                x += num;
            }
        }
        System.out.print(x + "," + y);
    }
}

全部评论

相关推荐

notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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