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
精通CSS(第2版)
[英] Andy Budd、[英] Simon Collison、[英] Cameron Moll / 陈剑瓯 / 人民邮电出版社 / 2010-5 / 49.00元
本书汇集了最有用的CSS技术,介绍了CSS的基本概念和最佳实践,结合实例探讨了图像、链接和列表的操纵,还有表单设计、数据表格设计、纯CSS布局等核心CSS技术。此外,书中着眼于创建跨浏览器的技术,讨论了bug及其捕捉和修复技术,还将所有技术组合成两个精彩的实例,讲述这些技术的工作原理和实际用法。 本书适合具有HTML和CSS基础知识的读者阅读。一起来看看 《精通CSS(第2版)》 这本书的介绍吧!