如何增加月份循环。然后从那时开始显示
How to increment months loop. Then display from then
大家见鬼去吧!我是编程新手。我试图搜索与我正在寻找的内容类似的内容,但我无法完全理解类似的示例。
我想要做的是让用户输入若干年,将年转换为月。然后使用 months 变量在列表中显示从第 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。这里有一个link到简单直接的教程: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;
大家见鬼去吧!我是编程新手。我试图搜索与我正在寻找的内容类似的内容,但我无法完全理解类似的示例。
我想要做的是让用户输入若干年,将年转换为月。然后使用 months 变量在列表中显示从第 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。这里有一个link到简单直接的教程: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;