题解 | #坐标移动#

坐标移动

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

import java.util.ArrayList;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 ha   sNextLine 的区别
        String str = in.nextLine();
        int x = 0, y = 0;
        for (String s : str.split(";")) {
            if (s.length() <= 1 || s.length() > 3) {
                continue;
            }
            if (s.charAt(0) != 'A' && s.charAt(0) != 'W' && s.charAt(0) != 'S' &&
                    s.charAt(0) != 'D') {
                continue;
            }
            if (s.length() == 2) {
                if(s.charAt(1) > '9' || s.charAt(1) < '0'){
                    continue;
                }
            }
            if (s.length() == 3) {
                if (s.charAt(1) > '9' || s.charAt(0) < '0') {
                    continue;
                }
                if (s.charAt(2) > '9' || s.charAt(2) < '0') {
                    continue;
                }
            }
            int dis =Integer.parseInt(s.substring(1));
            switch(s.charAt(0)){
                case 'A':
                    x-=dis;
                    break;
                case 'D':
                    x+=dis;
                    break;
                case 'W':
                    y+=dis;
                    break;
                case 'S':
                    y-=dis;
                    break;
            }
        }
        System.out.printf(x+","+y);
    }
}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务