题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*; import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { //读取数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { //将读取的一行字符串根据“;”进行切分 String[] strings = str.split(";"); //定义int型的x和y int x = 0; int y = 0; //遍历strings数组 for (int i = 0; i < strings.length; i++) { //遍历数组中的每个元素的每个字符 for (int j = 0; j < strings[i].length(); j++) { //判断第一个字符 if (strings[i].charAt(0) == 'A') { //切分除第一个字符之外的字符串 String substring = strings[i].substring(1); int num = 0; //将子字符串转为int类型,如果可以转换,则修改x的值,如果不可以转换,则直接跳出循环 try { num = Integer.parseInt(substring); } catch (NumberFormatException e) { break; } x -= num; break; } else if (strings[i].charAt(0) == 'D') { String substring = strings[i].substring(1); int num = 0; try { num = Integer.parseInt(substring); } catch (NumberFormatException e) { break; } x += num; break; } else if (strings[i].charAt(0) == 'W') { String substring = strings[i].substring(1); int num = 0; try { num = Integer.parseInt(substring); } catch (NumberFormatException e) { break; } y += num; break; } else if (strings[i].charAt(0) == 'S') { String substring = strings[i].substring(1); int num = 0; try { num = Integer.parseInt(substring); } catch (NumberFormatException e) { break; } y -= num; break; } else { } } } System.out.println(x + "," + y); } } }