//方法1
let size = 8;
let temp1 = "", temp2 = "";
for (let i = 0; i < size; ++i) {
if (i % 2 === 0) {
temp1 += " ";
temp2 += "#";
} else {
temp1 += "#";
temp2 += " ";
}
}
for (let i = 0; i < size; ++i) {
console.log(i % 2 === 0 ? temp1 : temp2);
}
//方法2
let otherSize = 6;
for (let i = 0; i < otherSize; ++i) {
//因为console.log输出后自动回车, 而我现在又不知道怎么样就不回车,故用此法模拟输出不回车
let b = i % 2 === 0;
let tempString = b ? " " : "#";
for (let j = 1; j < otherSize; ++j) {
if (b) {
tempString += (j % 2) === 0 ? " " : "#";
} else {
tempString += (j % 2) === 0 ? "#" : " ";
}
}
console.log(tempString);
}
版权声明:本文为zhouwengong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。