如何制作for或while嵌套循环patern [英] How to make a for or while nested loop patern

查看:99
本文介绍了如何制作for或while嵌套循环patern的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!所以我一直在尝试使用嵌套 for或while循环创建下面的模式,但是我的代码似乎没有提出我需要的东西。事实上,它什么都没显示!



模式:

*

**

***

****

*****

******

***** **

******

*****

****

* **

**

*



以下是我一直在尝试的代码。



我尝试过:



for(var count = 1; count< ; 8; count + = 1;){

document.write(count +*
);



for(var count2 = 1; count< 8; count - = 1;){

document.write(count +*
);

}

}

Hi! So i have been trying to create the pattern below using nested for or while loops, however my code does not seem to put out what I need. In fact, It displays nothing!

Pattern:
*
**
***
****
*****
******
*******
******
*****
****
***
**
*

Below is the code I have been trying out.

What I have tried:

for (var count = 1; count < 8; count+= 1;) {
document.write(count + "*
");

for (var count2 = 1; count < 8; count -= 1;) {
document.write(count + "*
");
}
}

推荐答案

你可以这样做:



You could do something like this:

for (var count = 1; count <= 7; count++)
{
    for(var i = 0;i<count;i++)
    {
        document.write("*");
    }
    document.write("<br>");
}

for (var count = 6; count >=1 ; count--)
{
    for(var i = 0;i<count;i++)
    {
        document.write("*");
    }
    document.write("<br>");
}


这篇关于如何制作for或while嵌套循环patern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆