卡在 CS50x 上左对齐右对齐马里奥金字塔
stuck on CS50x left aligned to right aligned mario pyramid
我一直在尝试让我的金字塔从左对齐到右对齐,但我对如何做感到困惑。这是我正在使用的代码。
编辑:我已经更改了代码,但出现错误
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
//asks user for number between 1 and 8
height = get_int("please give height: ");
}
while (height < 1 || height > 8);
//prints rows (i)
for (int rows = 0; rows < height; rows++)
{
//prints spaces (j)
for (int spaces = 0; spaces < height - rows; spaces++)
{
printf(".");
}
printf("\n");
}
for (int hashes = 0; rows < height - rows; hashes++)
{
printf("#");
}
printf("\n");
}
user gets prompted and writes number between 1 and 8
user types 4
Expected
...#
..##
.###
####
Actual output
....
...
..
.
mario.c:24:48: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
mario.c:24:32: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
我正在尝试打印散列并使用行整数,但出于某种原因,错误提示它是未定义的整数。
我认为错误是因为您在第一个和第二个 for 循环中混淆了大括号 {}。看起来您正在尝试执行三个相互嵌套的循环;但是,您的第三个循环在第一个循环之外,因为您在第二个循环的代码中间有一个额外的 } 。变量 row 在第一个循环中声明,第三个循环不知道这意味着什么,因为它在第一个循环之外。
坚持 class 关于缩进的建议帮助我保持直截了当。
我一直在尝试让我的金字塔从左对齐到右对齐,但我对如何做感到困惑。这是我正在使用的代码。 编辑:我已经更改了代码,但出现错误
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
//asks user for number between 1 and 8
height = get_int("please give height: ");
}
while (height < 1 || height > 8);
//prints rows (i)
for (int rows = 0; rows < height; rows++)
{
//prints spaces (j)
for (int spaces = 0; spaces < height - rows; spaces++)
{
printf(".");
}
printf("\n");
}
for (int hashes = 0; rows < height - rows; hashes++)
{
printf("#");
}
printf("\n");
}
user gets prompted and writes number between 1 and 8
user types 4
Expected
...#
..##
.###
####
Actual output
....
...
..
.
mario.c:24:48: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
mario.c:24:32: error: use of undeclared identifier 'rows'
for (int hashes = 0; rows < height - rows; hashes++)
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
我正在尝试打印散列并使用行整数,但出于某种原因,错误提示它是未定义的整数。
我认为错误是因为您在第一个和第二个 for 循环中混淆了大括号 {}。看起来您正在尝试执行三个相互嵌套的循环;但是,您的第三个循环在第一个循环之外,因为您在第二个循环的代码中间有一个额外的 } 。变量 row 在第一个循环中声明,第三个循环不知道这意味着什么,因为它在第一个循环之外。
坚持 class 关于缩进的建议帮助我保持直截了当。