如何绘制一个以C表示的宽度和高度的空矩形?
我最终会得到大量的反对票,但我已经为这个合乎逻辑的练习挣扎了好几个小时,我希望能得到一些帮助。给定宽度和高度,编译器应该绘制一个矩形 因此,我决定使用for循环,这看起来是完成这项工作最聪明的方法 以下是我一直使用的代码:如何绘制一个以C表示的宽度和高度的空矩形?,c,loops,if-statement,C,Loops,If Statement,我最终会得到大量的反对票,但我已经为这个合乎逻辑的练习挣扎了好几个小时,我希望能得到一些帮助。给定宽度和高度,编译器应该绘制一个矩形 因此,我决定使用for循环,这看起来是完成这项工作最聪明的方法 以下是我一直使用的代码: #include <stdio.h> main() { int width, height, b, a; printf("Please insert the value of the width.\n"); scanf("%d", & wi
#include <stdio.h>
main()
{
int width, height, b, a;
printf("Please insert the value of the width.\n");
scanf("%d", & width);
printf("Please insert the value of the height.\n");
scanf("%d", & height);
for (a = 0; a != height; a++) {
// fill the width
for (b = 0; b != width; b++ ) {
if (a == 0) {
printf("*");}
else if (a == width-1) {
printf("*");
}
else if (b == height-1) {
printf("*");
}
else if (b == 0) {
printf("*");
}
else {
printf(" ");
}}
printf("\n");
}
}
我觉得我错过了什么,但这太令人困惑了。有人能告诉我为什么我的代码没有画一个矩形吗?您缺少应该打印no*的部分。那你还想印什么呢?可能是空白 所以你应该做一些类似的事情
if (cond1 || cond2 || cond3 ||cond4) {
printf("*");
} else {
printf(" ");
}
您缺少应打印*号的部分。那你还想印什么呢?可能是空白 所以你应该做一些类似的事情
if (cond1 || cond2 || cond3 ||cond4) {
printf("*");
} else {
printf(" ");
}
这是一个合乎逻辑的问题。必须在以下边界条件下打印* a==0 a==高度-1 b==0 b==宽度-1 此外,如果不打印边界*s,则需要打印空格[]以生成结构表单 有关更多的良好实践,请查看下面的代码和注释
#include <stdio.h>
int main() //put proper signature
{
int width = 0, height = 0, b = 0, a = 0; // initalize local variables
printf("Please insert the value of the width.\n");
scanf("%d", & width);
printf("Please insert the value of the height.\n");
scanf("%d", & height);
for (a = 0; a != height; a++) {
// fill the width
for (b = 0; b != width; b++ ) {
if ((a == 0) || (a == height-1) || (b == width-1) || (b == 0)){ // put all * printing condition in one place
//also, conditions, (a == height-1) and (b == width-1) to be used
printf("*");
}
else // if not to print *, print space
printf(" ");
}
printf("\n");
}
return 0; // add a return statement.
}
这是一个合乎逻辑的问题。必须在以下边界条件下打印* a==0 a==高度-1 b==0 b==宽度-1 此外,如果不打印边界*s,则需要打印空格[]以生成结构表单 有关更多的良好实践,请查看下面的代码和注释
#include <stdio.h>
int main() //put proper signature
{
int width = 0, height = 0, b = 0, a = 0; // initalize local variables
printf("Please insert the value of the width.\n");
scanf("%d", & width);
printf("Please insert the value of the height.\n");
scanf("%d", & height);
for (a = 0; a != height; a++) {
// fill the width
for (b = 0; b != width; b++ ) {
if ((a == 0) || (a == height-1) || (b == width-1) || (b == 0)){ // put all * printing condition in one place
//also, conditions, (a == height-1) and (b == width-1) to be used
printf("*");
}
else // if not to print *, print space
printf(" ");
}
printf("\n");
}
return 0; // add a return statement.
}
要打印矩形框,请执行以下操作:
for (a = 0; a != height; a++) {
// fill the width
for (b = 0; b != width; b++ ) {
if (a == 0 || a== height-1 ) {
printf("*");
}else{
if(b == 0 || b == width-1){
printf("*");
}else{
printf(" ");
}
}
}
// ok, the line has been filled, new line
printf("\n");
}
要打印矩形框,请执行以下操作:
for (a = 0; a != height; a++) {
// fill the width
for (b = 0; b != width; b++ ) {
if (a == 0 || a== height-1 ) {
printf("*");
}else{
if(b == 0 || b == width-1){
printf("*");
}else{
printf(" ");
}
}
}
// ok, the line has been filled, new line
printf("\n");
}
您的IDEA很好,编写的代码直观易读。Mabye下次再多关注一下if语句,一切都会好起来的: 对于a=0;a!=身高a++{
// fill the width
for (b = 0; b != width; b++ ) {
if (a == 0) {
printf("*");
}
else if (a == height-1) {
printf("*");
}
else if (b == width-1) {
printf("*");
}
变量a等于height-1意味着最后一行将用*填充,width-1将填充最后一个colimn。将矩形视为一个矩阵您的想法很好,编写的代码直观易读。下次可能会更加关注if语句,一切都会很好: 对于a=0;a!=高度;a++{
// fill the width
for (b = 0; b != width; b++ ) {
if (a == 0) {
printf("*");
}
else if (a == height-1) {
printf("*");
}
else if (b == width-1) {
printf("*");
}
变量a等于高度-1意味着最后一行将用*填充,宽度-1将填充最后一行。将矩形视为矩阵请缩进该代码并确保其已编译。它已编译。我现在将缩进。谢谢。我真的很抱歉。编辑了问题。请缩进该代码并确保其已编译。它没有让我们编译。我现在缩进。谢谢。我真的很抱歉。编辑了这个问题。非常感谢。这很有意义。非常感谢。这很有意义。