题解 | #坐标移动#

坐标移动

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 安徽

相关推荐

点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
4
12
分享

创作者周榜

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