题解 | #坐标移动#

坐标移动

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

相关推荐

昨天 13:52
门头沟学院 后端
给🐭🐭个面试机会吧:嘿,mvbatis
点赞 评论 收藏
分享
01-24 08:13
已编辑
合肥工业大学 Java
程序员牛肉:没啥问题。标准的流水线简历,但是学历好一点,所以应该是有约面的机会的。 这段时间可以考虑把自己的两个项目彻底的理一理。争取能够讲清楚每一个功能点
点赞 评论 收藏
分享
评论
4
12
分享

创作者周榜

更多
牛客网
牛客企业服务