题解 | #坐标移动#

坐标移动

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

import java.util.*;
import java.io.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String s;
            while ((s = br.readLine())!=null){
                int x=0,y=0;
                String[] split = s.split(";");
                for (int i = 0; i < split.length; i++) {
                    if(!split[i].matches("[WASD][0-9]{1,2}")){
                        continue;
                    }
                    String s1 = split[i].substring(0,1);
                    String s2 = split[i].substring(1);
                
                    if( "A".equals(s1) ){
                        x-=Integer.valueOf(s2);
                    }else if("D".equals(s1)){
                        x+=Integer.valueOf(s2);
                    }else if("W".equals(s1)){
                        y+=Integer.valueOf(s2);
                    }else if("S".equals(s1)){
                        y-=Integer.valueOf(s2);
                    }else{
                        continue;
                    }
                }
                System.out.print(x);
                System.out.print(",");
                System.out.println(y);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

否极泰来来来来:解约赔多少
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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