题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        int x = 0, y = 0;


        String[] strings = line.split(";");
        for (int i = 0; i < strings.length; i++) {
            String cmd = strings[i];
            Pattern pattern = Pattern.compile("^([A|D|W|S])([0-9]{1,2})$");
            Matcher matcher = pattern.matcher(cmd);
            if (matcher.find()) {

                String cmdChar = matcher.group(1);
                int step = Integer.parseInt(matcher.group(2));

                //        A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动
                if (cmdChar.equals("A")) {
                    x += step * (-1);

                } else if (cmdChar.equals("D")) {
                    x += step;

                } else if (cmdChar.equals("W")) {
                    y += step;

                } else if (cmdChar.equals("S")) {
                    y += step * (-1);
                }
//                System.out.println(x+","+y);

            } else {
//                System.out.println("错误指令" + cmd);
            }


        }

        System.out.println(x + "," + y);
    }


}

全部评论
正则提取指令和步骤
点赞 回复 分享
发布于 2023-02-02 16:02 上海

相关推荐

“校招”、“3-5年经验”
xiaolihuam...:逆向工程不是搞外挂的吗,好像现在大学生坐牢最多的就是诈骗罪和非法侵入计算机系统罪,发美金,还居家办公,就是怕被一锅端,
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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