C++中? : 运算符的使用
比较两个数的大小:#include "stdafx.h"#include<iostream>using namespace std;int main() {int a, b;cout << "请输入两个数字:"; //输入两个数字,并以enter键结束每个数字cin >> a >> b;//在控制台输入两个数//使用? : 运算符判断a<b
·
比较两个数的大小:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main() {
int a, b;
cout << "请输入两个数字:"; //输入两个数字,并以enter键结束每个数字
cin >> a >> b; //在控制台输入两个数
//使用? : 运算符判断
a<b ? cout << a << "小于" << b << endl : cout << a << "大于" << b << endl;
return 0;
}
控制台运行结果:
三个值求最大值与最小值:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main() {
int a, b, c, d, max, min;
cout << "请输入三个数字:";//输入三个数字,并以enter键结束每个数字
cin >> a >> b >> c;
//使用? : 运算符判断
max = (d = a >= b ? a : b) >= c ? d : c;
cout << "最大值为:" << max << "\n" << endl;
//使用? : 运算符判断
max = (d = a <= b ? a : b) <= c ? d : c;
cout << "最小值为:" << max << "\n" << endl;
return 0;
}
控制台运算:

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)