题解 | #坐标移动#

坐标移动

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

import java.util.*;

public class Main {
  public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String input = in.nextLine();
      String[] array = input.split(";");

      int x = 0;
      int y = 0;
      for (String str : array) {
          // 去除空字符串
          if (str.trim().equals("")) {
              continue;
          }

          // 获取移动方向
          String way = String.valueOf(str.charAt(0));
          // 移动距离
          int size = 0;

          try{
                size = Integer.parseInt(str.substring(1));
          } catch (NumberFormatException e) {
              continue;
          }

          switch(way) {
              case "A":
                  x -= size;
                  break;
              case "D":
                  x += size;
                  break;
              case "W":
                  y += size;
                  break;
              case "S":
                  y -= size;
                  break;
              default:
                  break;
          }
      }

      System.out.println(x + "," + y);

  }

}

全部评论
try..catch妙啊
点赞 回复 分享
发布于 2023-08-06 21:21 安徽

相关推荐

争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
4
12
分享

创作者周榜

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