1

*编辑:在意识到我是个笨蛋后修复了代码

修复了有效的代码:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
int one, two, three = 0, highnum, lownum;
cout << "Enter your first integer: ";
cin >> one;
cout << "\nEnter your second integer: ";
cin >> two;
if (one > two)
{
    highnum = one;
    lownum = two;
}

while (one != -99 && two != -99 && three != -99)
{
    cout << "\nEnter integers until you want to stop (-99 to stop): ";
    cin >> three;
        if (three > one && three > two || three < one && three < two )
        { 
            if (three > one && three > two && three > lownum)
            {
                highnum = three;
            }
            else if ( three < one && three < two && three < lownum)
            {
                lownum = three;
            }
        }
        else if (one > three && one > two || one < three && one < two)
        {
            if (one > three && one > two)
            {
                highnum = one;
            }
            else if (one < three && one < two)
            {
                lownum = one;
            }
        }
        else if ( two > three && two > one || two < one && two < three)
        {
            if ( two > three && two > one)
            {
                highnum = two;
            }
            else if (two < one && two < three)
            {
                lownum = two;
            }
        }
}

cout << "Your lowest number is: "<< lownum << endl << "Your highest number is: " << highnum << endl << endl;
return 0;
}

我不确定数组是否是解决这类问题的方法,因为我们还没有在我们的讲座中学习它们,但是我很难找到这种循环结构背后的逻辑以及如何存储无限数量的变量直到输入 -99。任何帮助表示赞赏

赋值文本:编写一个带有循环的程序,让用户输入一系列整数。用户应输入 -99 表示系列结束。输入所有数字后,程序应显示输入的最大和最小数字。

到目前为止,我已经采用了两种不同的方法,使用了 while 和 for 循环的不同组合,但到目前为止,还没有骰子。有人有什么建议吗?

这是代码的两个不同版本

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()

/*
int one, two, three=0;
cout << "Enter your first integer: ";
cin >> one;
cout << "Enter you second integer: ";
cin >> two;

while ( one != -99 && two != -99 && three != -99 )
{
  cout << "Enter another integer. To stop the program enter -99: ";
  cin >> three;
}
if  (one < two && three)
  cout << one << endl;
else if (two < one && three)
  cout << two << endl;
else if (three < one && two)
  cout << three << endl;

return 0;
*/

这是我的第二次尝试:

int number, number2, number3, counter = 1;
double mul = 1;

cout << "Enter your first number that is not -99";
cin >> number;

while (number !=-99)
{
    cout << "Please enter your second number " <<endl<<endl;
    cin >> number2;
}

for (number != -99; number != -99; counter ++)
{
    cout <<"Please enter another number. ";
    cin >> number3;
}

if (number < number2 && number3)
{
    cout << "The low number is " << number << endl;
    if (number2 < number3)
    cout << "The high number is " << number3 << endl;
}
else if (number2 < number && number3)
{
    cout<< "The low number is " << number2 << endl;
    if (number < number3)
    cout << "The high number is " << number3 << endl;
}
else if (number3 < number && number2)
{
    cout << "The low numer is " << number3 << endl;
    if (number < number2)
    cout << "The high number is " << number2 << endl;
}
4

3 回答 3

3

你的代码和你的问题标题有点争议。

根据我对您问题的了解,您输入一系列数字,当输入 -99 时,程序将输出 min,max;但是您的代码不断输入 2 个数字,比较它们并产生输出,直到输入 -99。

这只是我的原始代码,尚未测试。

int main() {
    int max, min, number;

    cout << "Enter your number (not -99): ";
    cin >> number;
    max = number;
    min = number;

    while(1) {
        cout << "Enter your number (-99 to stop): ";
        cin >> number;
        if (number == -99) { break; }
        if (max < number) { max = number; }
        if (min > number) {min = number; }
    }
    cout << "max: " << max << endl;
    cout << "min: " << min << endl;

    return 0;
}
于 2013-10-25T03:01:25.507 回答
1

如果用户不输入大于或小于 1000000 的数字,则以下代码应该可以工作。

int max,min,input;

max = -1000000;
min = 1000000;

cout << "Enter number: ";
cin >> input;

while(input!=-99){
    if(input<min) min = input;
    if(input>max) max = input;

    cout << "Enter number: ";
    cin >> input;
}

cout << "Max: " << max << endl;
cout << "Min: " << min << endl;
于 2013-10-25T03:06:18.620 回答
0

/我今晚遇到了这个问题并想出了这个解决方案/

#include <iostream>
using namespace std;
int main()

{
int inputNum, lownum, highnum;

cout << "Enter as many numbers as you'd like and then I'll show you which\n";
cout << "was highest and which was lowest.\n";
cout << "Enter -99 to end program.\n\n=>";
cin >> inputNum;

//If user enters -99 right away
if (inputNum == -99)
{
    cout << "Ok, we'll thanks for playing anyway\n";
    cin.ignore(); //This & next line pause program after ending.
    cin.get(); 
    return 0;
}

//assign first input number to both high & low values
highnum = inputNum;
lownum = inputNum;


while (inputNum != -99)
{
    cout << "Input number\n=>";
    cin >> inputNum;

    if (inputNum == -99)
    {
        break;
    }

    if (inputNum > highnum)
        highnum = inputNum;

    if (inputNum < lownum)
        lownum = inputNum;
}

cout << "Low number was " << lownum << endl;
cout << "High number was " << highnum << endl;

cin.ignore(); //This & next line pause program after ending.
cin.get();
return 0;

}
于 2014-04-22T04:18:17.457 回答