题解 | #坐标移动#

坐标移动

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

感这题还是比较简单的,考察正则表达式的应用以及字符串处理,Java 类库熟悉的话,很快就能完成。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String line = in.next();
            String[] ops = line.split(";");
            int x = 0;
            int y = 0;
            for(String op : ops) {
                if(op.matches("[WASD][1-9]?[0-9]")) {
                    int move = Integer.parseInt(op.substring(1));
                    switch(op.charAt(0)) {
                        case 'W': y += move;break;
                        case 'A': x -= move;break;
                        case 'S': y -= move;break;
                        case 'D': x += move;break;
                    }
                }
            }
            System.out.println(x + "," + y);
        }
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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