<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>
Js while循环结构
最新推荐文章于 2024-10-04 17:24:00 发布