题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner;
//本题我的解题思路是,根据题意知,有两种是有效输入,以x代表字母AWSD,y代表数字,
//开头的“xyy;”与“xy;”是一种,还有一种在中间和结尾的形式如“;xyy;”和“;xy;”是另一种有效输入
//对这两种形式进行特别处理,其他的直接省略即可
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//代表X和Y坐标
int[] XY ={0,0};
//go代表坐标改变的数额
int go = 0;
int i = 0;
//先对开头的符合条件的进行处理,不符合就跳过
if(fun1(str.charAt(i))){
if(fun2(str.charAt(i+1))){
if(fun2(str.charAt(i+2))){
if(str.charAt(i+3)==';'){
go = (str.charAt(i+1)-48)*10 + (str.charAt(i+2)-48);
XY = funmove(str.charAt(i),go,XY);
}
}
else if(str.charAt(i+2)==';') {
go = str.charAt(i + 1) - 48;
XY = funmove(str.charAt(i), go, XY);
}
}
}
//对不是开头的符合条件的进行处理,不符合条件的跳过
for (i = 1; i <= str.length() - 3; i++) {
if(fun1(str.charAt(i))){
if(fun2(str.charAt(i+1))){
if(fun2(str.charAt(i+2))&& i+3 < str.length()){
if(str.charAt(i+3)==';'&&str.charAt(i-1)==';'){
go = (str.charAt(i+1)-48)*10 + (str.charAt(i+2)-48);
XY = funmove(str.charAt(i),go,XY);
}
}
else if(str.charAt(i+2)==';'&&str.charAt(i-1)==';') {
go = str.charAt(i + 1) - 48;
XY = funmove(str.charAt(i), go, XY);
}
}
}
}
System.out.print(XY[0]);
System.out.print(",");
System.out.print(XY[1]);
}
//判断是否为AWSD中的一个
public static boolean fun1(char ch) {
if (ch == 'A' || ch == 'W' || ch == 'S' || ch == 'D')return true;
else return false;
}
//判断是否为数字
public static boolean fun2(char ch1) {
if (ch1 >= 48 && ch1 <= 57 )return true;
else return false;
}
//对符合条件的进行特殊处理,并把得到的改变值返回
public static int[] funmove(char ch1,int go,int[] a){
switch (ch1) {
case 'A': {
a[0] -= go;
break;
}
case 'W': {
a[1] += go;
break;
}
case 'D': {
a[0] += go;
break;
}
case 'S': {
a[1] -= go;
break;
}
}
return a;
}
}

