千灯裕花园二期论坛:这题帮我看看(C语言)

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/24 02:20:07
#include<stdio.h>
void main()
{
int i = 1;
i++;
switch(i)
{
case 1:
case 9:printf("*********\n");
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:printf("* *\n");
}
}

想用循环语句输出以下图形:
*********
* *
* *
* *
* *
* *
* *
* *
* *
*********
请问我的程序有什么错误的地方?

你这道程序输出的只有
**

这里没有用到循环,只是单步执行了一次i++;
改成

#include<stdio.h>
void main()
{
int i ;
for (i=0,i<10,i++)
{
switch(i)
{
case 0:printf("*********\n"); break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:printf("* *\n");break;
case 9:printf("*********\n");
} //switch end
} //for end
}//main end

这样就可输出

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

先不说你要实现什么,还是说说你现在应该学会最基本的格式
首先,你没限制i的范围
其次,你没让switch语句中case break;
switch(i)
{
case 1; break;
defult;
}

我觉得应该把二楼高手的程序case 9还为default语句.
这样程序就很完整了!

你没有设置循环阿