Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为组成方框的星号创建程序?_Javascript_Html - Fatal编程技术网

Javascript 如何为组成方框的星号创建程序?

Javascript 如何为组成方框的星号创建程序?,javascript,html,Javascript,Html,它应该看起来像上面的星号排列。我是编程新手。到目前为止,我写了: ******* * * * * * * * ******* var-maxCount=7; var迭代计数=0; while(迭代计数

它应该看起来像上面的星号排列。我是编程新手。到目前为止,我写了:

*******
*     *
*  *  *
*     *
*******

var-maxCount=7;
var迭代计数=0;
while(迭代计数<最大计数){
迭代计数=迭代计数+1;
文件。写入(“*”);
}
到目前为止,我有一种强烈的感觉,那是不正确的,即使它是正确的,我也不知道如何从那里继续下去。 我知道我必须使用for和嵌套循环,但我非常困惑

试试看

<!DOCTYPE html>
<html>
<meta charset="UTF-8">

<body>

<script>

var maxCount = 7;
var iterationCount = 0;

while (iterationCount < maxCount) {
  iterationCount = iterationCount + 1;
  document.write('*');
}
var宽度=7,
高度=7,//例如
boxElem=document.getElementById(“box”);//这是用来装箱子的元件
对于(变量i=0;i
“应该是这样的。”嗯,确切地说是什么?你是说一个正方形的星号吗?是空心正方形,还是实心正方形?请具体说明。“到目前为止,我有一种强烈的感觉,那是不正确的。”试试看,你的输出是什么样子的?请出示
var width = 7,
    height = 7, // for example
    boxElem = document.getElementById("box"); // this is the element which will hold your box

for(var i = 0; i < height; i++) {
    for(var j = 0; j < width; j++) {
        if(i === 0 || i === height - 1 || j === 0 || j === width - 1 || (i === Math.floor(height / 2) && j === Math.floor(width / 2))) {
            // check if it's the first or last row or column, or if it's the centre of the box
            boxElem.innerHTML += "*";
        } else {
            boxElem.innerHTML += " "; // otherwise, don't add an asterisk but an empty space
        }
    }
    boxElem.innerHTML += "\n"; // new line
}