题解 | #坐标移动#

坐标移动

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[] arr = in.nextLine().split(";");
        int xVal = 0;
        int yVal = 0;
       // System.out.println(arr.length);

        for(int i = 0; i < arr.length; i++) {
            //合法坐标为A(或者D或者W或者S) + 数字(两位以内)
            //{n,m} Matches n to m repetitions of the previous character or expression.
            if(!arr[i].matches("[ASWD][0-9]{1,2}")) {
                continue;
            }
            int val = Integer.valueOf(arr[i].substring(1)); // take the substring from 1

            if(arr[i].charAt(0) == 'A') {
                xVal -= val;
            } else if(arr[i].charAt(0) == 'D') {
                xVal += val;
            } else if(arr[i].charAt(0) == 'S') {
                yVal -= val; 
            } else if(arr[i].charAt(0) == 'W') {
                yVal += val;
            }
        }
        System.out.println(xVal+","+yVal);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
2024-12-21 01:36
电子科技大学 Java
牛客850385388号:员工福利查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务