题解 | #坐标移动#

坐标移动

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;
    }
}

全部评论

相关推荐

我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
程序员牛肉:这一眼假啊,基本上都是骗人的,不然就涉及到职位贪腐了,就像之前华为的OD事件,看你运气好不好了
点赞 评论 收藏
分享
05-12 17:00
门头沟学院 Java
king122:你的项目描述至少要分点呀,要实习的话,你的描述可以使用什么技术,实现了什么难点,达成了哪些数字指标,这个数字指标尽量是真实的,这样面试应该会多很多,就这样自己包装一下,包装不好可以找我,我有几个大厂最近做过的实习项目也可以包装一下
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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