我目前正在尝试使用 NetBeans IDE 编写一个 C 程序。目标是从用户那里获取他们想要在屏幕上绘制的棍子数量的输入,其中字母“I”是一根棍子,如for 循环。问题是,我使用 scanf 获取每一行的用户输入,但程序执行并运行但实际上似乎从未到达 for 循环,因为它不执行其中的任何打印语句。我的 IDE 没有在 for 循环中打印我的任何打印语句,这就是为什么我假设它没有到达它们。
我试过简单地假设用户输入的值,而不是使用 scanf,但这似乎也没有到达 for 循环。任何关于我可以做些什么来解决这个问题的意见,将不胜感激!
#include <stdio.h>
int main(void) {
int length1, length2, length3;
int inputRow1, inputRow2, inputRow3;
printf("Please enter the number of Sticks you would like per row\n");
scanf("%d%d%d", inputRow1, inputRow2, inputRow3);
for (length1 = 0; length1 < inputRow1; length1++) {
printf("I");
for (length2 = 0; length2 < inputRow2; length2++) {
printf("I");
for (length3 = 0; length3 < inputRow3; length3++) {
printf("I");
return (0);
}
}
}
}
在使用 scanf
时,您应该使用 &
将值存储在变量中。
改变
scanf("%d%d%d",inputRow1,inputRow2,inputRow3);
到
scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
并且您的return
语句应该在退出main()
之前。
例如:
#include <stdio.h>
int main()
{
int length1,length2,length3;
int inputRow1,inputRow2,inputRow3;
printf("Please enter the number of Sticks you would like per row\n");
scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
for(length1=0;length1<inputRow1; length1++)
{
printf("I");
for(length2=0;length2<inputRow2;length2++)
{
printf("I");
for(length3=0;length3<inputRow3;length3++)
{
printf("I");
}
printf("\n");
}
printf("\n");
}
printf("\n");
return(0);
}
我是一名优秀的程序员,十分优秀!