每当我运行程序时,它直到换行符之后才开始打印星号,为什么? 这是我的代码:
int main()
{
int createRow = 0,
createCol;
while (createRow <= 5)
{
while (createCol <= ((createRow * 2) - 1))
{
printf("*");
createCol++;
}
createCol = 1;
createRow++;
printf("\n");
}
return 0;
}
输出:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
如您所见,在第一个星号之前有一个换行符。我该如何解决这个问题?
最佳答案
createCol
未初始化并在赋值之前使用。
进行以下更改
int createRow = 1,
createCol = 0;
while (createRow <= 5)
{
while (createCol <= ((createRow * 2)-1))
{
//Loop body
}
createCol = 0;
// Rest of the code
}
关于c - 出现不需要的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28650228/