题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String str = in.nextLine();
String[] strings = str.split(";");//得到字符串数组
int x = 0;//记录横坐标
int y = 0;//记录纵坐标
for (String s : strings) {//遍历
if (s.startsWith("A")) {//开头为A的情况
int len = s.length();//得到字符串长度
try {
int move = Integer.parseInt(s.substring(1));//看A后面是否为整数,如果是,则移动
x -= move;
} catch (NumberFormatException e) {
continue;//格式错误,跳过
}
}
if (s.startsWith("D")) {
int len = s.length();
try {
int move = Integer.parseInt(s.substring(1));
x += move;
} catch (NumberFormatException e) {
continue;
}
}
if (s.startsWith("W")) {
int len = s.length();
try {
int move = Integer.parseInt(s.substring(1));
y += move;
} catch (NumberFormatException e) {
continue;
}
}
if (s.startsWith("S")) {
int len = s.length();
try {
int move = Integer.parseInt(s.substring(1));
y -= move;
} catch (NumberFormatException e) {
continue;
}
}
continue;//如果不是以A,W,S,D开头,则跳过。
}
System.out.println(x+","+y);//输出结果
}
}

科大讯飞公司氛围 472人发布