问答详情
源自:6-11 编程练习

switch 换成if怎么写

switch 换成if怎么写

提问者:慕粉0943041209 2017-04-01 13:37

个回答

  • Limargin
    2017-04-01 16:24:13
    已采纳

    var day = Date.getDay();//拿到今天的日期,取值0-6,代表星期天到星期六
    switch (day) {
                case 1:
                    console.log("星期一")
                    break;
                case 2:
                    console.log("星期二")
                    break;
                case 3:
                    console.log("星期三")
                    break;
                case 4:
                    console.log("星期四")
                    break;
                case 5:
                    console.log("星期五")
                    break;
                case 6:
                    console.log("星期六")
                    break;
                default:
                    console.log("星期天")
                    break;
            }
            
    //if
    if(day==0){
                console.log("今天是星期天")
            }else if(day==1){
                console.log("今天是星期一")
            }else if(day==2){
                console.log("今天是星期二")
            }else if(day==3){
                console.log("今天是星期三")
            }else if(day==4){
                console.log("今天是星期四")
            }else if(day==5){
                console.log("今天是星期五")
            }else(day==6){
                console.log("今天是星期六")
            }