题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner; /** * @author hll[yellowdradra@foxmail.com] * @date 2022-10-06 23:16 **/ public class Main { static int x = 0; static int y = 0; static final char A = 'A'; static final char D = 'D'; static final char W = 'W'; static final char S = 'S'; public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); for (int i; (i = str.indexOf(';')) != -1; str = str.substring(i + 1)) { move(str.substring(0, i)); } System.out.println(x + "," + y); } static void move(String moveInfo) { if (moveInfo == null || moveInfo.length() == 0) { return; } char direction = moveInfo.charAt(0); int dist = 0; if (!(direction == A || direction == D || direction == W || direction == S)) { return; } try { dist = Integer.parseInt(moveInfo.substring(1)); } catch (NumberFormatException e) { return; } switch (direction) { case A : x -= dist; break; case D : x += dist; break; case W : y += dist; break; case S : y -= dist; break; default : break; } } }
#华为笔试#