您当前的位置:首页 > IT编程 > C++
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 |

自学教程:C++ 实例 - 判断三个数中的最大数

51自学网 2023-01-09 16:01:56
  C++
这篇教程C++ 实例 - 判断三个数中的最大数写得很实用,希望能帮到您。

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

实例 - 使用 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.38.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.38.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.38.3-4.2最大数为: 8.3

C++ 实例 - 判断元音/辅音
C++ 实例 - 求一元二次方程的根
51自学网自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1