C 分钟和最大
C++ min and max
本文关键字:分钟 更新时间:2023-10-16
我正在尝试获取一系列整数的最小数量和最大数字,并且我能够使用此代码获得最小值,但不确定我在做什么错。
#include <iostream>
#include <climits>
using namespace std;
int main()
{
//Declare variables.
int number, max, min;
//Set the values.
max = INT_MIN;
min = INT_MAX;
cout << "Enter -99 to end series" << endl;
while (number != -99)
{
//Compare values and set the max and min.
if (number > max)
max = number;
if (number < min)
min = number;
//Ask the user to enter the integers.
cout << "Enter a number in a series: " << endl;
cin >> number;
}
//Display the largest and smallest number.
cout << "The largest number is: " << max << endl;
cout << "The smallest number is: " << min << endl;
system("pause");
return 0;
}
问题在于您的非原始数字。当您第一次输入WARE循环时,该程序将采用任何数字值(尚未初始化的值,因此可以是任何东西),以与Max和Min进行比较。然后,将您的下一个比较与您的非初始化价值进行比较。
要解决此问题,只需将用户输入在while while循环之前。
cout << "Enter -99 to end series" << endl;
//Ask the user to enter the integers.
cout << "Enter a number in a series: " << endl;
cin >> number;
while (number != -99)
{
//Compare values and set the max and min.
if (number > max)
max = number;
if (number < min)
min = number;
//Ask the user to enter the integers.
cout << "Enter a number in a series: " << endl;
cin >> number;
}
相关文章:
- Boost::posix_time::ptime舍入到给定的分钟数
- 填孔方法需要20分钟以上
- 提升,以分钟精度获得 UTC 时间
- 对于使用高级服务的分钟数和应付金额,我不断得到相同的数字
- 有没有简单的方法可以将"02:00"转换为分钟,比如 120?
- 将秒转换为小时、分钟、秒.百分之一秒
- 在 Flashdevelopment OpenFL 中编译需要 31 分钟
- 以C++为单位将毫秒转换为小时:分钟:秒:毫秒
- 使用 OpenCV C++ 每 1 分钟创建新的视频文件
- 每秒清除一次终端,但留下几分钟
- Poco Websocket工作了50分钟后
- 进程以状态 -1073741571 终止(0 分钟、3 秒)
- 仅将纪元字符串保存到 mysql 日期时间年/月/日,但没有小时分钟秒
- WinAPI BitBlt冻结几分钟
- 高频插入 PostgreSQL,插入的数据要到几分钟后才能选择?
- 从当前时间减去 40 分钟
- 执行/在循环中停留一段时间,例如 1 分钟
- 将时间转换为小时、分钟和秒
- 如何在将秒转换为分钟且余数小于 10 秒时包含前导零
- fopen 几分钟后无法打开文件