题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { int[] start = new int[]{0, 0}; HashMap<Character, Integer[]> actions = new HashMap<>(); actions.put('D', new Integer[]{1, 0}); actions.put('A', new Integer[]{-1, 0}); actions.put('W', new Integer[]{0, 1}); actions.put('S', new Integer[]{0, -1}); Scanner in = new Scanner(System.in); String positions = in.nextLine(); String[] positionArray = positions.split(";"); for (String s : positionArray) { try { Integer[] integers = actions.get(s.charAt(0)); int parseInt = Integer.parseInt(s.substring(1)); start[0] += integers[0] * parseInt; start[1] += integers[1] * parseInt; } catch (Exception ignored) { } } System.out.println(Arrays.toString(start).replace("[", "").replace("]", "").replace(" ","")); } }