题解 | #坐标移动#

坐标移动

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做出来的

#悬赏#
全部评论

相关推荐

点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务