题解 | #坐标移动#

坐标移动

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        String[] steps = input.split(";");
        int[] location = new int[2];
        for (int i = 0; i < steps.length; i++) {
            if(steps[i].length() < 2){
                continue;
            }
            for(int j = 1; j < steps[i].length(); j++){
                char c = steps[i].charAt(j);
                if(c < '0' || c > '9'){
                    i++;
                    break;
                }
            }
            if(i == steps.length){
                break;
            }
            char direction = steps[i].charAt(0);
            if(steps[i].charAt(0) == 'A'){
                location[0] -= Integer.parseInt(steps[i].substring(1));
            }else if(steps[i].charAt(0) == 'D'){
                location[0] += Integer.parseInt(steps[i].substring(1));
            }else if(steps[i].charAt(0) == 'W'){
                location[1] += Integer.parseInt(steps[i].substring(1));
            }else if(steps[i].charAt(0) == 'S'){
                location[1] -= Integer.parseInt(steps[i].substring(1));
            }
        }
       System.out.print(location[0] + "," + location[1]);
    }
}

全部评论

相关推荐

10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
HNU_fsq:建议直接出国,这简历太6了。自愧不如
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务