题解 | #坐标移动#

坐标移动

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);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String line = in.nextLine();
        String[] items = line.split(";");
        int[] xy = new int[items.length];
        for (int i = 0; i < items.length; i++) {
            String item = items[i];
            if (item == null || item.length() < 2) {
                continue;
            } 
            char c = item.charAt(0);
            int n = 0;
            try {
                n = Integer.parseInt(item.substring(1));
            } catch(NumberFormatException e) {
                continue;
            }
            if ('A' == c) {
                xy[0] -= n;
                continue;
            }
            if ('D' == c) {
                xy[0] += n;
                continue;
            }
            if ('W' == c) {
                xy[1] += n;
                continue;
            }
            if ('S' == c) {
                xy[1] -= n;
                continue;
            }

        }
        System.out.print(xy[0] + "," + xy[1]);
    }
}

全部评论

相关推荐

09-24 18:30
已编辑
长春工业大学 产品经理
小肥罗:HR就是好人的缩写哈哈哈哈
点赞 评论 收藏
分享
🐭🐭不想面试啦�...:哈哈哈哈,是hc稀缺是吧。一边稀缺,一边后端简历多到捞不出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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