题解 | #坐标移动#

坐标移动

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 strPoint = in.nextLine();

        String[] strArr = strPoint.split(";");

        int x = 0;
        int y = 0;

        for(int i = 0; i < strArr.length; i++){
            String s1 = strArr[i];

            if(s1 == null || s1.length() == 0){
                continue;
            }
            String startStr = s1.substring(0,1);

            switch(startStr){
                case "W":
                    String numStr = s1.substring(1);
                    try{
                        int length =  Integer.parseInt(numStr);
                        y = y + length;
                    }catch(Exception e){

                    }
                    break;
                case "S":
                    String numStr2 = s1.substring(1);
                    try{
                        int length =  Integer.parseInt(numStr2);
                        y = y - length;
                    }catch(Exception e){

                    }
                    break;

                case "A":
                    String numStr3 = s1.substring(1);
                    try{
                        int length =  Integer.parseInt(numStr3);
                        x = x - length;
                    }catch(Exception e){

                    }
                    break;

                case "D":
                    String numStr4 = s1.substring(1);
                    try{
                        int length =  Integer.parseInt(numStr4);
                        x = x + length;
                    }catch(Exception e){

                    }
                    break;
            }
            
        }

        System.out.println(x + "," + y);

        
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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