gpt4 book ai didi

c++ - 如何实现递增数据的循环

转载 作者:行者123 更新时间:2023-11-28 04:24:33 26 4
gpt4 key购买 nike

<分区>

我试图通过在 C++ 中使用循环来使我的程序更智能。试图解决这个问题时我遇到了精神障碍,因为我试图跟踪这么多计数器。比正确的代码更重要的是,我如何正确地思考问题以得出正确的结论?

我已经尝试了 for 循环并认为嵌套的 for 循环可能是必要的,但我不知道如何不断添加 monthDays[] 值以正确检查输入并做出正确的决定。

输入是小于 366 但大于 0 的用户定义数字。该程序旨在返回月份名称和该月份过去的天数。

string monthNames[12] = {"January", "February", "March", "April"};
int monthDays[12] = {31, 28, 31, 30};


if ((input - monthDays[0]) <= 0){

cout << monthNames[0] << " " << input;
}

else if (input < (monthDays[0] + monthDays[1]) &&
(input > monthDays[0]){

cout << monthNames[1] << " " << (input - monthDays[0]);
}

else if (input < (monthDays[0] + monthDays[1] + monthDays[2]) &&
input > (monthDays[0] + monthDays[1])){

cout << monthNames[2] << " " << (input - monthDays[0] - monthDays[2]);
}

else if (input < (monthDays[0] + monthDays[1] + monthDays[2] + monthDays[3]) &&
input > (monthDays[0] + monthDays[1] + monthDays[2])){

cout << monthNames[2] << " " << (input - monthDays[0] - monthDays[2] - monthDays[3]);
}

该程序可以运行,但可以简化。我应该如何改变我的思维过程来实现循环?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com