Js while循环结构

<body>
  <script>
    // // 打印十行字符串
    // let a = 1
    // // ()里面是循环的条件,条件成立,执行{}里面的操作
    // while(a<=10){
    //   console.log("第"+a+"天,我要好好学习,天天向上");
    //   // 在while中要对循环的变量重新赋值,否则会死循环
    //   a+=1
    // }

    // whlie循环联系1
    // 找出100以内,能被3整除的数,并输出
    // let num = 0
    // let num1 = 1
    // while (num<=100) {
    //   if (num%3 == 0) {
    //     console.log("第"+num1+"个,能被3整除的数是"+num);
    //     num1+=1
    //   }
    //   num+=1
    // }

    // whlie循环联系2
    // 找出21世纪所有的闰年并打印出来
    // let year = 2000
    // let num = 1
    // while (year<2100) {
    //   if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    //     console.log(year+"年是21世纪的第"+num+"个闰年");
    //     num+=1
    //   }
    //   year+=1
    // }

    // whlie循环联系3
    // 输入年月日,算出该日是全年的第多少天
    let year = parseInt(prompt("请输入年份:"))
    let month = parseInt(prompt("请输入月份:"))
    let day = parseInt(prompt("请输入日期:"))
    let num = 1 //用于表示循环的月数
    let num1 = 0 //用于表示总天数
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
      console.log("这年是闰年,2月有29天");
      while (num<month) {
        if (num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12) {
          num1+=31
        }else if (num==4 || num==6 || num==9 || num==11) {
          num1+=30
        }else{
          num1+=29
        }
        num+=1
      }
    } else {
      console.log("这年不是闰年,2月有28天");
      while (num<month) {
        if (num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12) {
          num1+=31
        }else if (num==4 || num==6 || num==9 || num==11) {
          num1+=30
        }else{
          num1+=28
        }
        num+=1
      }
    }
    console.log(`${year}年${month}月${day}是 ${year}年的第${num1+day}天`);
  </script>
</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值