我在为我的某个作业创建模式程序时遇到了困难。教师希望我们创建一个程序,要求用户输入宽度和高度,然后根据输入的内容打印出“*”的模式。我能够打印整个矩形,但她希望它是一个特定的模式。前两行和最后一行应该用星星填充,两者之间的线条应该与我在下面提供的示例相似。必须遵循的规则是宽度必须是6的倍数,高度必须更大
模式示例:
111111111111111111111111111111111111
111111111111111111111111111111111111
111 111 111 111 111 111
111 111 111 111 111 111
111 111 111 111 111 111
111111111111111111111111111111111111
111111111111111111111111111111111111
我的代码:
int width, height, i, j, space;
//Program asks user to enter width of their fabric
printf("What is the width of your fabric?\n");
scanf("%d", &width);
while(width % 6 !=0){
printf("Please enter a width that is a multiple of 6\n");
scanf("%d", &width);
}
//Program asks user to enter height of their fabric
printf("What is the height of your fabric?\n");
scanf("%d", &height);
while(height <= 6 || height % 2 == 0){
printf("Please enter an odd height which is at least 7\n");
scanf("%d", &height);
}
for(i=1; i<=height; i++)
{
for(j=1; j<=width; j++)
{
printf("*");
}
printf("\n");
}
答案 0 :(得分:0)
在循环中需要一个if语句并根据打印的行(变量i)调整宽度。 您可能希望将i重命名为currentRow,将j重命名为currentColumn。
答案 1 :(得分:0)
你可以试试这个。变量flip
在备用线上将'xxx'模式翻转为'xxx'。
int flip = 1;
for (int i = 1; i <= height; i++)
{
if (i <= 2 || i >= height - 1)
{
// the top two & bottom two lines are solid '*'
for (int j = 1; j <= width; j++)
{
printf("*");
}
}
else
{
flip = 1 - flip;
for (int j = 1; j <= width; j++)
{
// repeating pattern: three '*' followed by three ' '
// Divide column by 3 (no remainder) and see whether the result is odd or even
// subtract 1 so each group of 3 has same result when divided by 3
if ( ((j-1)/3) % 2 == flip )
printf("*");
else
printf(" ");
}
}
printf("\n");
}
答案 2 :(得分:-1)
#include <stdio.h>
#include <math.h>
int main()
{
int index=90;
float mynumber[index];
float scalar=2.25;
mynumber[abs(scalar)]=1;
printf("%f\n",mynumber[abs(scalar)]);
return 0;
}