gpt4 book ai didi

c - 我们怎样才能将三元运算符与尽可能多的 else-if 一起使用?

转载 作者:行者123 更新时间:2023-11-30 21:14:14 27 4
gpt4 key购买 nike

基本的三元运算符用法是

(condition) ? True part : False part

我们如何添加“multiple-else-if”功能?

最佳答案

链接三元运算符时,格式化是使其可读的关键:

尝试在一行长线上完成所有操作会导致难以理解。

int result = (oper=='+')?  a+b : 
(oper=='-')? a-b :
(oper=='/')? a/b :
(oper=='*')? a*b :
(oper=='^')? a^b :
(oper=='&')? a&b :
0;

或者,格式化 Zafeer 的示例:

#include <stdio.h>
int main(){
for(int i = 1; i<=10; i++)
printf("%d%s\n",i, (i==1)? "st":
(i==2)? "nd":
(i==3)? "rd":
"th");
}

关于c - 我们怎样才能将三元运算符与尽可能多的 else-if 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909511/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com