题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] move = str.split(";");
        int x=0;
        int y=0;
        for (int i = 0; i < move.length; i++) {
            if (move[i].length()<2||move[i].length()>3){
                continue;
            }
            if ("A".equals(move[i].substring(0,1))){
                x-=chToInt(move[i].substring(1));
            }
            if ("D".equals(move[i].substring(0,1))){
                x+=chToInt(move[i].substring(1));
            }
            if ("W".equals(move[i].substring(0,1))){
                y+=chToInt(move[i].substring(1));
            }
            if ("S".equals(move[i].substring(0,1))){
                y-=chToInt(move[i].substring(1));
            }
        }
        System.out.println(x+","+y);



    }
    public static int chToInt(String str){
        try {
            return Integer.parseInt(str);
        }catch ( Exception e){
            return 0;
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务