题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str  =  in.nextLine();
        String[] arr = str.split(";");
        System.out.println(move(arr));
    }
    private static String move(String[] arr) {
        int x=0,y=0;
        for(String str : arr){
            if(str == null || str.length()==0 || isError(str)){
                continue;
            }
            int tmp = Integer.valueOf(str.replaceAll("[A-Z|a-z]+",""));
            if(str.startsWith("A")){
                x-=tmp;
            }
            if(str.startsWith("D")){
                x+=tmp;
            }
            if(str.startsWith("W")){
                y+=tmp;
            }
            if(str.startsWith("S")){
                y-=tmp;
            }
        }
        return x+","+y;
    }

    private static boolean isError(String str) {
       if(!str.matches("[A|S|D|W]{1}[0-9]+")){
        return true;
       }
       return false;
    }

    
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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