题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

public class Main {
    //HJ17 坐标移动
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String input = scanner.nextLine();
            String[] chars = input.split(";");
            //定义坐标
            int x = 0;
            int y = 0;
            for(int i=0 ; i<chars.length ; i++) {
                String s1 = "";
                if(chars[i]==null||chars[i]==""||chars[i].trim().length()==0){
                    continue;
                }
                     s1 = chars[i].substring(0, 1);
                    int s2;
                    
                    try {
                         s2 = Integer.parseInt(chars[i].substring(1));
                    }catch (Exception e){
                        continue;
                    }
                        if ("A".equals(s1)) {
                            x = x - s2;
                        } else if ("S".equals(s1)) {
                            y = y - s2;
                        } else if ("W".equals(s1)) {
                            y = y + s2;
                        } else if ("D".equals(s1)) {
                            x = x + s2;
                        }
                }

            System.out.println(x+","+y);
        }
    }
}
全部评论

相关推荐

13 5 评论
分享
牛客网
牛客企业服务