我正在尝试创建一个程序,该程序显示提示的最小和最大整数,但提示用户。最大值始终是正确的,但最小值始终为0。不知道我的代码中缺少什么?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int intsWanted, min, max, input;
do {
cout << "How many integers would you like to
enter?" << endl;
cin >> intsWanted;
} while (intsWanted < 1);
cout << "Please enter " << intsWanted << " integers." << endl;
cin >> min;
min = max;
intsWanted--;
while (intsWanted >= 1) {
cin >> input;
if (input > max) max = input;
if (input < min) min = input;
intsWanted--; }
cout << "min: " << min << endl;
cout << "max: " << max << endl;
return 0;
}
答案 0 :(得分:2)
将行min = max更改为max = min即可解决您的问题!