题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static Integer left = 0;
    private static Integer right = 0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        String[] split = line.split(";");
        for (String str : split) {
            Main.check(str);
        }
        System.out.println(left + "," + right );
    }

    public static void check(String str) {
        //判断字符串
        int length = str.length();
        if (length <= 3 && length >= 2) {
            int value = -1;
            switch (str.charAt(0)) {
                case 'A':
                    value = Main.getValue(str.substring(1));
                    if (value != -1) {
                        left -= value;
                    }
                    break;
                case 'D':
                    value = Main.getValue(str.substring(1));
                    if (value != -1) {
                        left += value;
                    }
                    break;
                case 'W':
                    value = Main.getValue(str.substring(1));
                    if (value != -1) {
                        right += value;
                    }
                    break;
                case 'S':
                    value = Main.getValue(str.substring(1));
                    if (value != -1) {
                        right -= value;
                    }
                    break;

            }
        }
        return;
    }
    public static int getValue(String str) {
        //获取数值
        int length = str.length();
        if (length == 1) {
            char ch = str.charAt(0);
            if (ch >= '1' && ch <= '9') {
                return ch - '0';
            }
        } else {
            char ch1 = str.charAt(0);
            char ch2 = str.charAt(1);
            if (ch1 >= '0' && ch1 <= '9' && ch2 >= '0' && ch2 <= '9') {
                int a = ch1 - '0';
                int b = ch2 - '0';
                return a * 10 + b;
            }
        }
        return -1;
    }
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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