#坐标移动#一般解题思路

坐标移动

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);
        // 注意 hasNext 和 hasNextLine 的区别
        int x = 0;
        int y = 0;
        String [] strs = in.nextLine().split(";");
        for( int i = 0;i < strs.length ;i ++){
            String str = strs[i];
            if(str != null && str.length() > 1){
                String tmpstr = str.substring(1);
                Integer tmp;
                try{
                    tmp = Integer.parseInt(tmpstr);
                }catch (Exception e){
                    tmp = null;
                }
                if(tmp != null ){
                     if(str.startsWith("A")){
                        x -= tmp;
                    }else if(str.startsWith("D")){
                        x += tmp;
                    }else if(str.startsWith("W")){
                        y += tmp;
                    }else if(str.startsWith("S")){
                        y -= tmp;
                    }
                }
               
            }
        }
        System.out.println(x + "," + y);
    }

    
}

全部评论

相关推荐

投递拓竹科技等公司10个岗位
点赞 评论 收藏
分享
快点约我面试吧
投递百度等公司10个岗位
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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