随机矩形作为数组

时间:2016-01-02 18:41:28

标签: javascript

当我将此代码作为html运行时,矩形将无法正常显示

任何想法都能获得正确的结果?

感谢您的帮助

<!DOCTYPE html>
<html>
  <head>
    <link href="css/styles.css" rel="stylesheet">
  </head>
  <body>

    <h1>My Website</h1>

    <p>This is my first website</p>
<script>
var w=(Math.floor(Math.random()*10))+2;
document.write("width:"+(w+1));
var h=(Math.floor(Math.random()*10))+3;
document.write("height:"+h);
var cua = new Array;
cua[0]="|";
cua[w]="|";

for (i=1;i<w;i++){
    cua[i]=" ";
}
//base y techo
var bas = new Array;

for (k=0;k<(w+1);k++){
    bas[k]="-";
}

document.write(bas.join(""));//techo

for (j=1;j<(h+1);j++){
    document.write(cua.join(""));
}
//join quita las comas
document.write(bas.join(""));//base

  </script>
  </body>
</html>     

1 个答案:

答案 0 :(得分:0)

插入<br>以换行,&nbsp;插入空格 这就是你需要的东西:

&#13;
&#13;
<!DOCTYPE html>
<html>
  <head>
    <link href="css/styles.css" rel="stylesheet">
  </head>
  <body>

    <h1>My Website</h1>

    <p>This is my first website</p>
<script>
var w=(Math.floor(Math.random()*10))+2;
document.write("width:"+(w+1));
var h=(Math.floor(Math.random()*10))+3;
document.write("height:"+h);
var cua = new Array;
cua[0]="|";
cua[w]="|";

for (i=1;i<w;i++){
    cua[i]="&nbsp;";
}
//base y techo
var bas = new Array;

for (k=0;k<(w+1);k++){
    bas[k]="-";
}
document.write("<br>");
document.write(bas.join(""));//techo
document.write("<br>");
for (j=1;j<(h+1);j++){
    document.write(cua.join(""));
     document.write("<br>");
}
//join quita las comas
document.write(bas.join(""));//base

  </script>
  </body>
</html>     
&#13;
&#13;
&#13;