比较循环中的变量 [英] comparing variables in loops

查看:80
本文介绍了比较循环中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在循环中比较两个以上的变量?找出最大的最小数字这里是我的代码,它从用户那里得到10个输入..现在我怎样才能找到它们之间的最大值(最大值)和最小值(最小值)?????



how can i compare more than two variables in loops? to find out the largest an minimum number here is my code which takes 10 inputs from the user .. Now how can i find out maximum(largest) and minimum (smallest) between them?????

#include<iostream>
using namespace std;

int main()

{
 int max=0,min=0;
 int num;
 int counter =0;
 cout<<"Enter 10 numbers"<<endl;
 while(counter<10)
 {

     cin>>num;
     counter++;
 }
     return 0;
 }

推荐答案

您可以根据需要进行比较。你需要用代码重写这个,所以它是有道理的。通常,您为最大值创建一个变量,为最小值创建一个变量,如果新值更好,则替换当前值。
You can compare as many as you want. You need to reword this, with code, so it makes sense. Typically, you create a variable for the biggest and one for the smallest, and every one, you replace the current value if the new one is a better match.


#include<iostream>
#include<climits>
using namespace std;

int main()
{
 int max=INT_MIN,min=INT_MAX;
 int num;
 int counter =0;
 cout<<"Enter 10 numbers"<<endl;
 while(counter<10)
 {

     cin>>num;
     if ( max < num) max = num;
     if ( min > num) min = num;
     counter++;
 }
 cout << "min " << min << ", max " << max << endl;
 return 0;
 }


这篇关于比较循环中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆