题解 | #坐标移动#

坐标移动

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);
        String str = in.nextLine();
        String[] arr = str.split(";");
        int x = 0, y = 0;
        for(String a : arr){
            if(!a.matches("[WASD][0-9]{1,2}")){
                continue;
            }
            int val = Integer.valueOf(a.substring(1));
            switch (a.charAt(0)){
                case 'W':
                    y = y + val;
                    break;
                case 'S':
                    y = y - val;
                    break;
                case 'A':
                    x = x - val;
                    break;
                case 'D':
                    x = x + val;
                    break;
            }
        }
        System.out.println(x+","+y);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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