题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int len = str.length(); if(len<1 || len>10000) return; int[] pos = {0,0}; String[] arr = str.split(";"); for(String s: arr){ if("".equals(s) || " ".equals(s)) continue; char c = s.charAt(0); if(c!='W' && c!='A' && c!='S' && c!='D') continue; String numStr = s.substring(1); if(!isNum(numStr)) continue; //c numStr合法,以下可用 // int n = Integer.parseInt(numStr); int n = Integer.valueOf(numStr); //两种方法字符串转基本数据类型 if(c == 'W'){ pos[1] += n; }else if(c == 'A'){ pos[0] -= n; }else if(c == 'S'){ pos[1] -= n; }else { //=='D' pos[0] += n; } } System.out.println(pos[0] + "," + pos[1]); } public static boolean isNum(String str){ for (int i=0; i<str.length(); i++){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } }