简体   繁体   English

查找最大值和最小值

[英]Find Max & Min values

I am trying to find the minimum and maximum value. 我正在尝试找到最小值和最大值。 I am running into following two issues: 我遇到了以下两个问题:

  1. When entering only negative numbers, the max value will be equal to 0, not the input's maximum value. 仅输入负数时,最大值将等于0,而不是输入的最大值。
  2. I have a similar issue when entering only positive numbers. 仅输入正数时我也遇到类似的问题。 Basically the smallest value will be 0 not the input's value. 基本上,最小值将是0而不是输入的值。

Any tips would be appreciated. 任何提示将不胜感激。

#include <iostream>
using namespace std;

int main()
{
    int input;
    int max = 0;
    int min = 0;

    cout << "Enter number: ";
    while (input != -1)
    {
        cin >> input;
        if(input != -1)
        {
            if(input > max)
            {
                max = input;
            }

            if(input < min)
            {
                min = input;
            }
        }
    }
    cout <<"Max: " << max << " Min " << min << endl;
}

From your code I guessed that there will be no negative number. 从您的代码中,我猜不会有负数。 Then do something like this: 然后执行以下操作:

EDIT :This part from another answer will be good. 编辑 :从另一个答案这部分会很好。

include <iostream>
using namespace std;

int main()

{
    int input;
    int hasInput = 0;
    int max = std::numeric_limits<int>::min();
    int min = std::numeric_limits<int>::max(); //#include <limits>

    cout << "Enter number: ";
    while (cin >> input)
    {
        if(input == -1) break;
        hasInput = 1;
        if(input > max)
        {
            max = input;
        }
        if(input < min)
        {
            min = input;
        }
     }
        if(hasInput == 1)
        cout <<"Max: " << max << " Min " << min << endl;
 }

At the beginning, instead of setting to 0, ask for the first number before the while loop and set min and max to that number. 首先,不要将其设置为0,而应在while循环之前要求第一个数字,并将min和max设置为该数字。 As a side note, using -1 as a quit condition for something which accepts numerical input might lead to problems, you might want to ask them for the number of numbers beforehand or detect non numerical input to end the program. 附带说明一下,对于接受数字输入的内容,使用-1作为退出条件可能会导致问题,您可能需要事先向他们询问数字数量,或者检测非数字输入以结束程序。

EDIT: example code: 编辑:示例代码:

int main(int argc, char** argv)
{
int input;
bool hasInput = false;
cout << "Enter number: ";
cin >> input;
int max = input;
int min = input; 

cout << "Enter number: ";
while (cin >> input)
{
    if(input == -1) break;
    hasInput = true;
    if(input > max)
    {
        max = input;
    }
    if(input < min)
    {
        min = input;
    }
    cout << "Enter number: ";
 }
    if(hasInput)
    cout <<"Max: " << max << " Min " << min << endl;
}

Forgot to check if the first input is -1... im sure you can figure that one out. 忘了检查第一个输入是否为-1 ...我确定您可以算出一个。

Just like @Turix said, the problem is because of your initial max and min values. 就像@Turix所说的那样,问题出在您的初始maxmin Your max value should be the smallest possible integer value so that anything is greater than it, similarly your min should be the largest possible integer value. 您的max应为可能的最小整数值,以使任何大于该整数的值类似,您的min应为最大可能的整数值。 With this in hand, intialize your variables like this: 有了这个,像这样初始化您的变量:

int max = std::numeric_limits<int>::min();
int min = std::numeric_limits<int>::max(); //#include <limits>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM