题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] split = s.split(";"); HashMap<Integer, Integer> hs = new HashMap<>(); hs.put(0,0);//横向坐标存值 hs.put(1,0);//纵向坐标存值 for (int i = 0; i < split.length; i++) { String s1 = split[i]; if(s1.length()>1&&s1.length()<4) { char c = s1.charAt(0); String sa = String.valueOf(c); String sn = s1.substring(1); boolean b = sn.chars().allMatch(Character::isDigit); int i1=0; if (!b) { continue; } i1 = Integer.parseInt(sn); if (i1>=0 && i1<100){ switch (sa){ case "A": hs.put(0,hs.get(0)-i1); break; case "S": hs.put(1,hs.get(1)-i1); break; case "W": hs.put(1,hs.get(1)+i1); break; case "D": hs.put(0,hs.get(0)+i1); break; } } } } String success=hs.get(0)+","+hs.get(1); System.out.println(success); } }