题解 | #坐标移动#
坐标移动
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); // 注意 hasNext 和 hasNextLine 的区别 int x=0; int y=0; String s=in.nextLine(); String[] str=s.split(";"); for(String ope : str){ if( ope == null || ope.trim().length() == 0) { continue; } char a=ope.charAt(0); int data=0; try{data=Integer.valueOf(ope.substring(1));} catch(Exception e){ continue; } // switch(a){ // case 'A': // x-=data; // case 'D': // x+=data; // case 'W': // y+=data; // case 'S': // y-=data; // } if(a=='A')x-=data; if(a=='D')x+=data; if(a=='S')y-=data; if(a=='W')y+=data; } System.out.println(x+","+y); } }
为什么switch不管case有没有匹配上都会执行后面的语句??最后用if做出来的
#悬赏#