嵌套三元运算符 [英] nested ternary operator

查看:91
本文介绍了嵌套三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有状态变量有三个值1,2,3

1.已批准

2.拒绝
3.Pending

我使用嵌套的三元operator.how我可以使用它吗?

解决方案

尝试:< pre lang =c#> string result = status == 1 已批准:status == 2 拒绝 待定;



但是,我会改用开关:

 开关(状态)
{
案例 1 :result = 已批准; break ;
case 2 :result = 拒绝; break ;
case 3 :result = 待定; break ;
默认:result = ERROR< /跨度>; break ;
}


状态=(y.Status == 1)? 已批准:((y.Status == 2)?拒绝:待定)


hello
I have status variable which has three valuse 1,2,3
1.Approved
2.Reject
3.Pending
I wand to use nested ternary operator.how can i use it?

解决方案

Try:

string result = status == 1 ? "Approved" : status == 2 ? "Reject" : "Pending";


However, I would use a switch instead:

switch (status)
   {
   case 1: result = "Approved"; break;
   case 2: result = "Reject"; break;
   case 3: result = "Pending"; break;
   default: result = "ERROR"; break;
   }


Status = (y.Status == 1) ? "Approved" : ((y.Status == 2) ? "Reject" : "Pending")


这篇关于嵌套三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆