题解 | #坐标移动#

坐标移动

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int len = str.length();
        if(len<1 || len>10000) return;

        int[] pos = {0,0};
        String[] arr = str.split(";");
        for(String s: arr){
            if("".equals(s) || " ".equals(s)) continue;
            char c = s.charAt(0);
            if(c!='W' && c!='A' && c!='S' && c!='D') continue;
            String numStr = s.substring(1);
            if(!isNum(numStr)) continue;
            //c numStr合法,以下可用
            // int n = Integer.parseInt(numStr);
            int n = Integer.valueOf(numStr); //两种方法字符串转基本数据类型
            if(c == 'W'){
                pos[1] += n;
            }else if(c == 'A'){
                pos[0] -= n;
            }else if(c == 'S'){
                pos[1] -= n;
            }else { //=='D'
                pos[0] += n;
            }
        }
        System.out.println(pos[0] + "," + pos[1]);
    }

    public static boolean isNum(String str){
        for (int i=0; i<str.length(); i++){ 
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
08-27 21:03
已编辑
西南石油大学 Java
冷花幽露:大概率是了,京东面试就是这样。我上周一面也是20多分钟,面试官问的很刁钻的问题也答上来了,面完过了几天还是没推进,泡池子,昨天一看挂了。如果一面完第2天没有收到2面邀请,基本上不用抱希望了。如果你的bg是985,面试流程也是和我们一样,20多分钟,唯一区别就是面完他们会很快收到二面邮件,而不像我们泡池子然后挂掉
点赞 评论 收藏
分享
09-02 14:53
... 前端工程师
双尔:露头就秒,骗你的,不露也秒
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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