题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.useDelimiter("\n");
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int[] xy = new int[] {0, 0};
            String s = in.next();
            String[] split = s.split(";");
            for (String move : split) {
                if (null == move || "".equals(move) || move.length() < 2) {
                    continue;
                }
                char dir = move.charAt(0);
                String distance = move.substring(1);
                int distanceInt = 0;
                try {
                    distanceInt = Integer.parseInt(distance);
                } catch (Exception e) {
                    continue;
                }

                if ('A' == dir) {
                    xy[0] -= distanceInt;
                    continue;
                }
                if ('S' == dir) {
                    xy[1] -= distanceInt;
                    continue;
                }
                if ('W' == dir) {
                    xy[1] += distanceInt;
                    continue;
                }
                if ('D' == dir) {
                    xy[0] += distanceInt;
                }
            }
            System.out.println(xy[0] + "," + xy[1]);
        }
    }
}

全部评论

相关推荐

肥沃富饶:可能初创公司,老板不懂技术
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务