【发布时间】:2015-04-23 08:12:32
【问题描述】:
谁能帮我写代码。它应该做的是当我输入“create 6 5”时,它应该创建一个带有 * 的 6x5 矩形,但除非我输入“show”,否则它不会显示。
这是我正在处理的代码,它还不正确,仍在弄清楚如何去做。
我是编程新手。
char s[100];
char *s1 = "show";
char *s2 = "create";
int main()
{
int zWidth = 0, zHeight = 0, zLoop = 0, aLoop = 0;
do
{
scanf("%s %d %d", &s, &zWidth, &zHeight);
}
while (strcmp(s1, s) != 0);
if(strcmp(s2, s) == 0)
{
//To draw the first horizontal line
for(zLoop = 0; zLoop < zWidth; zLoop++)
printf("*");
printf("\n");
//Drawing the vertical lines
for(zLoop = 2; zLoop < zHeight; zLoop++)
{
printf("*");
for(zLoop = 0; aLoop < zWidth-2; aLoop++)
printf("*");
printf("*\n");
}
//Last horizontal Line
for(zLoop = 0; zLoop < zWidth; zLoop++)
printf("*");
printf("\n");
}
return 0;
}
【问题讨论】:
-
只有当您为
scanf输入show时,do...while循环才会中断。只有这样while(strcmp(s1, s) != 0) 中的条件才会为假。一旦你输入show,那么if(strcmp(s2, s) == 0)就永远不会是真的 -
scanf("%s %d %d", &s, &zWidth, &zHeight);应该是scanf("%s %d %d", s, &zWidth, &zHeight);。但这并不能解决您的问题。 -
尝试使用
while (strcmp(s2, s) != 0);,然后将if(strcmp(s2, s) == 0)替换为do{scanf("%s",s);}while(strcmp(s1,s)!=0); -
for(zLoop = 0; aLoop < zWidth-2; aLoop++)应该是for(aLoop = 0; aLoop < zWidth-2; aLoop++) -
@IAMLaw
for(zLoop = 0; zLoop < zHeight; zLoop++) { for(aLoop = 0; aLoop < zWidth; aLoop++) printf("*"); printf("\n"); }