*编辑:在意识到我是个笨蛋后修复了代码
修复了有效的代码:
#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;
}