当我使用"while"时输出错误 [英] Error with output when I was using 'while'

查看:311
本文介绍了当我使用"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


这篇关于当我使用"while"时输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆