5条回答 默认 最新
- benbenli 2021-05-10 05:33关注
你的变量 a 是定义在同一scope,但是你定义了2次,编译器好像还每理解你的意图 (可能因为加了括号的缘故),所以它说还每定义。这样写吧,运行通过了。其实还要加上输入错误检测,年份范围,月份1-12, 天也要在每月天数范围等。
#include <iostream> using namespace std; int main() { int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int leap_month_days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; cout << "please enter year, month and day, and press ENTER:"; int y, m, d; cin >> y >> m >> d; int* md = (y % 4 == 8 && y % 100 != 0 || y % 400 == 0) ? leap_month_days : month_days; int s = d; for (int i = 0; i < m - 1; ++i) { s += md[i]; } cout << "It's the " << s << "th day in the year." << endl; return 0; } // Output please enter year, month and day, and press ENTER:2021 5 9 It's the 129th day in the year. please enter year, month and day, and press ENTER:2021 01 30 It's the 30th day in the year. please enter year, month and day, and press ENTER:2020 3 1 It's the 60th day in the year.
如果你满意我的回答,请点采纳,我很感谢你的认可。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用 1