在 C++ 中,我需要创建一个程序,当输入某个数字时循环并停止,然后显示最大值和最小值
Posted
技术标签:
【中文标题】在 C++ 中,我需要创建一个程序,当输入某个数字时循环并停止,然后显示最大值和最小值【英文标题】:In C++, I need to create a program that loops and stops when a certain number is entered, Then displays the max and min 【发布时间】:2013-10-25 02:49:01 【问题描述】:*edit: 在意识到我是个笨蛋后修正了代码
有效的固定代码:
#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;
【问题讨论】:
提示:输入时不要使用三个变量,只使用1然后使用数组。 你发帖说你遇到了麻烦。为我们跟进:您在哪里遇到问题? 现在它只是了解循环过程背后的逻辑,并在不使用数组的情况下存储无限变量。不过,我可能已经想通了,一秒钟…… 您实际上并不需要存储无限数量的值。您只需要一个用于当前值的变量,一个用于最小值,一个用于最大值。如果 min 的变量用 MAX_INT 初始化,max 的变量用 MIN_INT 初始化,那么通过比较当前值和它们应该很容易在循环期间保持它们的最新状态。 【参考方案1】:你的代码和你的问题标题有点争议。
根据我对您问题的了解,您输入一系列数字,当输入 -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;
【讨论】:
【参考方案2】:如果用户不输入大于或小于 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;
【讨论】:
【参考方案3】:/我今晚遇到了这个问题,想出了这个解决方案/
#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;
【讨论】:
以上是关于在 C++ 中,我需要创建一个程序,当输入某个数字时循环并停止,然后显示最大值和最小值的主要内容,如果未能解决你的问题,请参考以下文章
当用户输入除整数以外的任何内容时,为啥我的程序会无限循环输出? C++ [重复]