题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String s = scanner.nextLine();
String[] in = s.split(";");
int x = 0;
int y = 0;
boolean falg = false;
for(int i = 0 ; i<in.length;i++){
String sb = in[i];
if(!isValid(sb)){
continue;
}else{
char operate = sb.charAt(0);
int num = Integer.parseInt(sb.substring(1));
if(operate == 'A'){
x -= num;
}else if(operate == 'D'){
x+=num;
}else if(operate == 'W'){
y+=num;
}else if(operate == 'S'){
y-=num;
}
}
}
System.out.printf(x+","+y);
}
}
//判断格式是否为*123456789,*为任意字符
public static boolean isValid(String s){
if(s.length()<=1){
return false;
}
int count = 0;
for(int i = 1 ; i < s.length(); i++){
if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
count++;
}else{
return false;
}
}
return true;
}
}
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String s = scanner.nextLine();
String[] in = s.split(";");
int x = 0;
int y = 0;
boolean falg = false;
for(int i = 0 ; i<in.length;i++){
String sb = in[i];
if(!isValid(sb)){
continue;
}else{
char operate = sb.charAt(0);
int num = Integer.parseInt(sb.substring(1));
if(operate == 'A'){
x -= num;
}else if(operate == 'D'){
x+=num;
}else if(operate == 'W'){
y+=num;
}else if(operate == 'S'){
y-=num;
}
}
}
System.out.printf(x+","+y);
}
}
//判断格式是否为*123456789,*为任意字符
public static boolean isValid(String s){
if(s.length()<=1){
return false;
}
int count = 0;
for(int i = 1 ; i < s.length(); i++){
if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
count++;
}else{
return false;
}
}
return true;
}
}