C++ 实例 - 判断三个数中的最大数

C++ 教程 · 2019-02-26 11:58:56

通过屏幕我们输入三个数字,并找出最大的数。

实例 - 使用 if

#include <iostream> using namespace std; int main() { float n1, n2, n3; cout << "请输入三个数: "; cin >> n1 >> n2 >> n3; if(n1 >= n2 && n1 >= n3) { cout << "最大数为: " << n1; } if(n2 >= n1 && n2 >= n3) { cout << "最大数为: " << n2; } if(n3 >= n1 && n3 >= n2) { cout << "最大数为: " << n3; } return 0; }

以上程序执行输出结果为:

请输入三个数: 2.3
8.3
-4.2
最大数为: 8.3

实例 - 使用 if...else

#include <iostream> using namespace std; int main() { float n1, n2, n3; cout << "请输入三个数: "; cin >> n1 >> n2 >> n3; if((n1 >= n2) && (n1 >= n3)) cout << "最大数为: " << n1; else if ((n2 >= n1) && (n2 >= n3)) cout << "最大数为: " << n2; else cout << "最大数为: " << n3; return 0; }

以上程序执行输出结果为:

请输入三个数,以空格分隔: 2.3
8.3
-4.2
最大数为: 8.3

实例 - 使用内嵌的 if...else

#include <iostream> using namespace std; int main() { float n1, n2, n3; cout << "请输入三个数: "; cin >> n1 >> n2 >> n3; if (n1 >= n2) { if (n1 >= n3) cout << "最大数为: " << n1; else cout << "最大数为: " << n3; } else { if (n2 >= n3) cout << "最大数为: " << n2; else cout << "最大数为: " << n3; } return 0; }

以上程序执行输出结果为:

请输入三个数,以空格分隔: 2.3
8.3
-4.2
最大数为: 8.3

点击查看所有 C++ 教程 文章: https://codercto.com/courses/l/18.html

查看所有标签

程序化广告实战

程序化广告实战

吴俊 / 机械工业出版社 / 2017-8-15 / 79.00元

中国程序化广告领域领袖级专家,私有化程序购买领域的布道者的一线实战笔记,宋星等近20位专家联袂推荐。从业务和技术双重视角系统讲解程序化广告的理论、知识、实践方法和关键要点。一起来看看 《程序化广告实战》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具