我最近一直在研究 C 语言,并决定尝试制作一个矩形程序。但是,我遇到了一个问题,似乎无法找到它存在的原因。
每次我输入许多行、列和一个符号时,它总是打印 10 列,其中包含正确的符号和行数。
这是我的代码。
#include <stdio.h>
int main()
{
int rows;
int columns;
char symbol;
printf("\nEnter # of rows: ");
scanf("%d", &rows);
printf("Enter # of columns: ");
scanf("%d", &columns);
scanf("%c");
printf("Enter a symbol: ");
scanf("%c", &symbol);
for(int i = 1; i <= rows; i++)
{
for(int j = 1; j <= columns; j++)
{
printf("%c", symbol);
}
printf("\n");
}
return 0;
}
这是输出。
PS D:\CFolder\NestedLoops> cd "d:\CFolder\NestedLoops\" ; if ($?) { gcc NestedLoops.c -o
NestedLoops } ; if ($?) { .\NestedLoops }
Enter # of rows: 3
Enter # of columns: 5
Enter a symbol: $
$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$
PS D:\CFolder\NestedLoops>
无论我输入的列号,它总是打印 10 列。
作为旁注,当我尝试运行程序但删除符号输入时它工作正常。