繁体   English   中英

如何增加月循环。 然后从那时开始显示

[英]How to increment months loop. Then display from then

地狱大家! 我是编程的菜鸟。 我试图搜索与所需内容相似的东西,但我无法完全理解相似的例子。

我正在尝试做的是让用户输入数年,然后将数年转换为数月。 然后使用月份变量在列表中显示从第1个月到全部月份。 我需要循环才能继续,直到达到整个月数,然后在此停下来。 以下是到目前为止我所知道的以及我所学到的。 我怀疑我可能需要使用某种类型的计数器变量,但不能完全确定该怎么做。

int main(){
int years, months;
printf("Enter years ");
scanf_s("%d", &years);

months = years * 12;
printf("Months is %d ", months); 

do {
    printf("Month", ); //Month 1,2,3,4........24 up to full amount that was converted from years//
} while ();
return 0;
int count = 1;
while (month != 0) {
   printf("%d, ", count);
   month = month - 1;
   count = count + 1;
} 

看一下while,for和do-while循环。 这是一个简单直接的教程的链接: https : //www.tutorialspoint.com/cprogramming/c_loops.htm

对于您的问题,只需创建一个以1初始化的额外变量并循环,直到它通过几个月的值。

int years = 0;
int months = 0;
int counter = 1;

printf("Enter years ");
scanf("%d", &years);

months = years * 12;
printf("Months is %d ", months);

printf("Months: ");

do {
    printf("%d ", counter); //Month 1,2,3,4........24 up to full amount that was converted from years//
    counter++;
} while (counter <= months);

return 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM