当我使用"while"时输出错误
问题描述:
输出未在直线中列出,我的代码在下面
我尝试过的事情:
The Output isn''t listed in a straight line, my code is below
What I have tried:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int noofpotato=1;
int day=1;
int month=1;
while(month<=12){
printf("Month:%d\t\t Day:%d\t\t Potato:%d\n",month,day,noofpotato);
++month;
day*=2;
}
return 0;
}
答
如果您需要正确对齐的数字(如果我知道的话),请尝试
If you need properly aligned numbers (if I got you), then try
#include <stdio.h>
#include <stdlib.h>
int main()
{
int noofpotato=1;
int day=1;
int month=1;
while(month<=12)
{
printf("Month:%2d\tDay:%4d\t Potato:%d\n",month,day,noofpotato);
++month;
day*=2;
}
return 0;
}
您可以执行如下所示的右填充操作
you can do something like right padding as below
printf("Month:%6d\t\t Day:%6d\t\t Potato:%d\n",month,day,noofpotato);
输出:
Output:
Month: 1 Day: 1 Potato:1
Month: 2 Day: 2 Potato:1
Month: 3 Day: 4 Potato:1
Month: 4 Day: 8 Potato:1
Month: 5 Day: 16 Potato:1
Month: 6 Day: 32 Potato:1
Month: 7 Day: 64 Potato:1
Month: 8 Day: 128 Potato:1
Month: 9 Day: 256 Potato:1
Month: 10 Day: 512 Potato:1
Month: 11 Day: 1024 Potato:1
Month: 12 Day: 2048 Potato:1