前端第一周,Javascript-- switch分支结构

  //switch分支语句(书写格式):

        /*  switch(需要判断的变量){

              case 值1:

                   判断的变量 和 值1 全等时 所执行的代码;

                   break;

           case 值2:

                  判断的变量 和 值2 全等时 所执行的代码;

                  break;

           case 值3:

                 判断的变量 和 值3 全等时 所执行的代码;

                 break;

           default:

           判断的变量 和 前面任何一个case的值 都不全等时,所执行的代码语句

       } */



 

        //switch分支语句

        //switch分支语句 进行的是:全等判断,成立条件是判断的变量 和 case的值必须全等(恒等)

        //首先,判断的变量 和 第一个case的值 进行比较,如果全等,则执行对应case下的代码

        //如果不全等,则继续向下一个case的值进行比较,依次操作执行

        //如果最终所有的case的值都不和判断的变量全等,则执行default下的代码语句


 

      //switch语句中break关键字是必须要写的,如果缺失了break就会发生switch穿透(case穿透)

        /*switch穿透

             switch穿透是指当switch语句中某一个case被匹配到并且case下的语句会被执行

             当case部分逻辑执行完毕之后如果没有break的话就谁一直往下一个case匹配

 

             也就是说在switch语句中当case被匹配到,程序只有遇到break才会跳出switch分支语句

             在某些情况下我们可以利用switch穿透 实现一些代码优化  */


 

        // 输入一个数字 1-12,表示月份,判断这个月有多少天?

        var month = +prompt('请输入要查询的月分,1-12');

        console.log(month);

        switch (month) {

            case 1:

            case 3:

            case 5:

            case 7:

            case 8:

            case 10:

            case 12:

                alert(month + '月有31天');

                break;

            case 2:

                alert(month + '月 平年有31天  闰年有29天');

                break;

            case 4:

            case 6:

            case 9:

            case 11:

                alert(month + '月有30天');

                break;

            default:

                alert('你输入的月份不存在');

        }


 

        /* 方法二:

         switch (month) {

             case 1:

                 alert(month + '月有31天');

                 break;

             case 2:

                 alert(month + '月 平年有31天  闰年有29天');

                 break;

             case 3:

                 alert(month + '月有31天');

                 break;

             case 4:

                 alert(month + '月有31天');

                 break;

             case 5:

                 alert(month + '月有31天');

                 break;

             case 6:

                 alert(month + '月有31天');

                 break;

             case 7:

                 alert(month + '月有31天');

                 break;

             case 8:

                 alert(month + '月有31天');

                 break;

             case 9:

                 alert(month + '月有30天');

                 break;

             case 10:

                 alert(month + '月有31天');

                 break;

             case 11:

                 alert(month + '月有30天');

                 break;

             case 12:

                 alert(month + '月有31天');

                 break;

             default:

                 alert('您输入的月份不存在');

         }  */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对数据库的增删改查,前端页面和后端数据 使用java对数据库进行基本操作 并设计前端页面。 package control; import service.UserServiceImpl; import vo.User; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; @WebServlet(name="userControl",urlPatterns = "/user") public class UserControl extends HttpServlet { private UserServiceImpl service=new UserServiceImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String action =req.getParameter("action"); switch (action){ case "query": queryAll(resp); break; case "add": add(req,resp); break; } } private void add(HttpServletRequest req, HttpServletResponse resp) { String username = req.getParameter("username"); String password = req.getParameter("password"); User user = new User(); user.setUsername(username); user.setPassword(password); boolean flag = service.add(user); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out =null; try{ out = resp.getWriter(); out.println(String.valueOf(flag)); }catch (Exception e){ e.printStackTrace(); } out.flush(); out.close(); } private void queryAll(HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); String json=null; try { json=service.queryAll(); } catch (SQLException e) { e.printStackTrace(); } PrintWriter out=resp.getWriter(); out.println(json); System.out.println(json); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值