JavaScript中while循环经典例题1
程序员文章站
2024-03-23 21:36:10
...
- 九九乘法表
//css样式
<style type="text/css">
table{
border-collapse:collapse;
}
td{
border:1px solid blue;
text-align: center;
color: darkolivegreen;
font-size: 20px;
padding: 10px 10px;
}
</style>
//代码
var row = 1;
document.write("<table border = 1px>");
while(row <= 9){
document.write("<tr>");
var col = 1;
while(col <= row){
document.write("<td>");
document.write(row + "*" + col + "=" + row * col);
col++;
document.write("</td>");
}
document.write("<br />");
document.write("</tr>");
row++;
}
document.write("</table>");
-
* ** *** **** *****
打印以上图案
var star = "*";
var row = 1;
while(row <= 5){
var col = 1;
while(col <= row){
document.write(star);
col++;
}
document.write("<br />");
row++;
}
- 求0000-20220所有 平年 + 所有闰年
var year1 = 0000;
var count1 = 0;//计数普通闰年
var count2 = 0;//计数世纪闰年
while(year1 <= 2020){
if(year1 % 4 == 0 && year1 % 100 != 0){
console.log(year1 + "为普通闰年");
count1++;
}else if(year1 % 400 == 0){
console.log(year1 + "为世纪闰年");
count2++;
}
year1++;
}
console.log(count1);
console.log(count2);
- 1-100奇偶数求和
var num = 1;
var sum = 0;
var odd = 0; //奇数
while(num <= 100){
if(num % 2 == 1){
console.log(num + "是奇数");
odd = odd + num;
}else{
console.log(num + "是偶数");
sum = sum + num;
}
num++;
}
- 操场大约一百多人,三个三个排剩一个,四个四个排剩二个,五个五个排剩二个;
var num = 100;
var flag = true;
while(flag){
if(num % 3 == 1 && num % 4 == 2 && num % 5 == 2){
console.log(num);
flag = false;
}
num++;
}
- 猜拳
var success = 0;
var lose = 0;
while (success < 2 && lose < 2 ){
var player = prompt("请输入你的猜拳");
if(player != "" && player != null){
var computer = Math.floor(Math.random() * 3);
switch (computer){
case 0:
computer = "剪刀";
break;
case 1:
computer = "石头";
break;
case 2:
computer = "布";
break;
}
console.log("computer = " + computer);
if(player == computer){
console.log("平局")
}else if (player == "剪刀" && computer == 2 || player == "石头" && computer == 0 || player == "布" && computer == 3){
console.log("玩家赢");
success++;
}else{
console.log("电脑赢");
lose++;
}
}else{
console.log("输入有误");
}
}
console.log(success);
if(success == 2){
console.log("玩家取得最终胜利");
}else{
console.log("电脑取得最终胜利");
}
- 水箱装水120立方,水龙头每小时进水12立方,每小时出水30立方,问:多少小时排完?
var hour = 0;
var water = 120;
while(water > 0){
hour++;
//water = 120 - (30 - 12) * hour;//我一开始的想法,思路没错,但是最好不要这样写
water += 18;
water -= 30;
}
console.log(hour);
- 甲乙丙丁四个人加工零件 加工的总零件数为370个
如果甲加工的零件数多10
如果已加工的零件数少20
如果丙加工的零件数乘以2
如果丁加工的零件数除以2
则四个人加工的零件数就相等 (//设为ave)
求四个人加工的零件个数分别是多少
var ave = 0;//Ave是四个人运算后相同的零件数
while(true){
if((ave - 10) + (ave + 20) + (ave / 2) + (ave * 2) == 370){
console.log("甲 = " + ave - 10);
console.log("乙 = " + ave + 20);
console.log("丙 = " + ave / 2);
console.log("丁 = " + ave * 2);
break;
}else{
ave++;
}
}
- 100元换零钱,有50元,20元、10元、5元、1元
var x = 0;//50元
var count1 = 0;//计数一共多少种方法
while(x <= 2){
var y = 0;//20元
while(y <= 5){
var z = 0;//10元
while(z <= 10){
var w = 0;//5元
while(w <= 20){
var o = 0;//1元
while(o <= 100){
if(50 * x + 20 * y + 10 * z + w * 5 + 1 * o == 100){
console.log(x, y, z, w, o);
count1++;
}
o++;
}
w++;
}
z++;
}
y++;
}
x++;
}
console.log(count1);
10.计算100-999之间的水仙花数
var num = 100;
while(num <= 999){
var handred = Math.floor(num / 100);
var ten = Math.floor(num % 100 / 10);
var indeviual = Math.floor(num % 10);
if(num == handred ** 3 + ten ** 3 + indeviual ** 3){
console.log(num);
}
num++;
}
- 图一:
图二:
var line = 6;//行
if(line == 6){
document.write("⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐");
line -= 1;
}
document.write("<br />");//line = 5;
while(line > 0){//line >= 0时 0也会进入empty循环,会输出11个月亮,即图二,而正确的输出结果应该是图一
var col = 1;//列
while (col <= line){
document.write("⭐");
col++;
}
var empty = 0;
while(empty < 2 * (5 - line + 1) - 1 ){
document.write("????");
empty++;
}
var col1 = 1;
while (col1 <= line){
document.write("⭐");
col1++;
}
document.write("<br />");
line--;
}
上一篇: Wait函数详解
下一篇: 如何kill同一个应用的所有进程