题解 | #坐标移动#

坐标移动

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

相关推荐

4 12 评论
分享
牛客网
牛客企业服务