题解 | #坐标移动#

坐标移动

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean isIlegal(String s){
        if(s.length()!=3&&s.length()!=2)
            return false;
        char[]ch=s.toCharArray();
        if(ch[0]!='A'&&ch[0]!='S'&&ch[0]!='W'&&ch[0]!='D')
            return false;
        if(!(ch[1]>='0'&&ch[1]<='9')){
            return false;
        }
        if(s.length()==3&&(ch[1]=='0'||!(ch[2]>='0'&&ch[2]<='9')))
            return false;
        return true;
    }
    public static int getNum(String s){
        char[]ch=s.toCharArray();
        int ans=1;
        if(s.length()==2)
            ans=ch[1]-'0';
        if(s.length()==3)
            ans=(ch[1]-'0')*10+ch[2]-'0';
        return ans;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x=0,y=0;
        String s=in.next();
        String []newStr=s.split(";");
        for(int i=0;i<newStr.length;i++){
            if(isIlegal(newStr[i])){
                char ch=newStr[i].charAt(0);
                if(ch=='A')
                 x=x-getNum(newStr[i]);
                else if(ch=='D')
                x=x+getNum(newStr[i]);
                else if(ch=='W')
                y=y+getNum(newStr[i]);
                else y=y-getNum(newStr[i]);
            }
        }
        System.out.print(x+","+y);
    }
}

全部评论

相关推荐

06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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