您好我正在尝试使用-99的输入显示最高和最低数字以打破循环并且不显示-99作为最低数字。任何帮助都会很棒。
int number;
int largeNum = 0 ;
int smallNum = 0;
cout << "enter numbers" ;
do
{
cin >> number;
if ( number > largeNum)
largeNum = number;
if ( number < largeNum)
smallNum = number;
if (number < smallNum)
smallNum = number;
} while (number != -99);
cout << largeNum << endl;
cout << smallNum << endl;
答案 0 :(得分:1)
你可以这样做:
do {
cin >> number;
if ( number == -99 ) {
break;
}
if ( number > largeNum)
largeNum = number;
if ( number < largeNum)
smallNum = number;
if (number < smallNum)
smallNum = number;
} while ( true );
答案 1 :(得分:0)
问题的更好解决方案是使用排序:
void main()
{
int array[100],n;
//get number of elements
cout<<"Enter number of elements: ";
cin>>n;
//get elements
for(int i=0;i<n;i++)
{
int temp;
cout<<"Enter element to position "<<i<<" :";
cin>>temp;
//to break at -99
if(temp==-99)
break;
else
array[i]=temp;
}
//sort the elements in ascending order
for(int i=0;i<n;i++)
{
if(array[i]>array[i+1])
{
int temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
//display smallesst and largest
cout<<"Smallest: "<<array[0];
cout<<"Largest: "<<array[n-1];
}