嗨我想用*打印这样的形状。 此形状的高度为6,如果输入另一个偶数作为高度,它将生成具有该高度的形状。C编程 - 星号形状
到目前为止,我这样做,但这种失败..... 有人会告诉我哪里做错了吗?谢谢!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int height;
int i, j;
printf("Please input height with an even integer ");
scanf("%d", &height);
for (i=height/2; i>=1; i--)
{
for (j=1; j<=height; j++)
{
if (i==height/2)
printf("*");
else if (j=i || j==(height-(i-1)))
printf("*");
else
printf(" ");
}
printf("\n");
}
system("PAUSE");
return 0;
}
谢谢大家!
其实我早先尝试了另一种方法,结果就出来了。
我首先打印出最上面一行和最下面一行,并将星号和空格放在应该出现的位置。
这是我的解决方案。
void printStar(int x);
int main(int argc, char *argv[])
{
int height, value = 2 , value2;
printf("Please input height with an even integer ");
scanf("%d", &height);
value2 = height-1;
for (int i=1; i<=height; i++)
{
if(i==1){
printStar(height);
}else if(i == height){
printStar(height);
}else{
for(int y = 1 ; y<=height ; y++){
if(value == y || value2 == y){
printf("*");
}else{
printf(" ");
}
}
value++;
value2--;
}
printf("\n");
}
system("PAUSE");
return 0;
}
void printStar(int x){
for(int i = 0 ; i<x ;i++){
printf("*");
}
}
尝试http://codereview.stackexchange.com/。或者使用调试器逐行执行代码。 – 2013-02-16 13:26:31
'else if(j = i || j ==(height-(i-1)))''你的意思是'j == i'? – Maroun 2013-02-16 13:32:34