【发布时间】:2021-11-29 05:28:55
【问题描述】:
我无法同时遍历两个值。我有一个正在编写的程序,我需要从输入的任何年份开始为接下来的 20 年中的每一年找到一个特定的假期,并且我目前无法查看给定年份的指定日期。我尝试在 while 循环中使用 while 循环,但它对我不起作用。
当前代码
#include <stdio.h>
#define SIZE 1000
int main() {
// update date opposite of year so go through all the days first then the year
// while loop that checks if the date was on a monday and gets all the mondays from the month then checks the second last one
int d = 15, m = 5, y, future, day, month, year, i = 0;
char dates[SIZE];
printf("Enter a year: ");
scanf("%d", &y);
future = y + 20;
while (15 <= d && d < 25 && y <= future) {
printf("\nDay: %d and Year: %d\n", d, y);
d++;
y++;
// day = d;
// printf("\nDay: %d and Year: %d\n",day,year);
// while(y <= future){
// printf("year: %d\n", y);
//check dates in year from previous while loop
// y++;
// year = y;
// printf("\nDay: %d and Year: %d\n",day,year);
// }
}
return 0;
}
这里的代码是两个条件都在一个while循环中我尝试在第一个while循环中将年份(y)条件移动到另一个while循环但无济于事。
电流输入和输出
Enter a year: 2015
Day: 15 and Year: 2015
Day: 16 and Year: 2016
Day: 17 and Year: 2017
Day: 18 and Year: 2018
Day: 19 and Year: 2019
Day: 20 and Year: 2020
Day: 21 and Year: 2021
Day: 22 and Year: 2022
Day: 23 and Year: 2023
Day: 24 and Year: 2024
期望的输出
Day: 15 and Year: 2015
Day: 16 and Year: 2015
Day 17: and Year: 2015
Day...24 and Year...2035
因此,在输入年份后最多 20 年的所有年份中,该程序应从第 15 天到第 24 天计数,但目前仅在 2024 年之前的年份中计数一天。
【问题讨论】:
标签: c math while-loop printf std