如何在不使用if语句的情况下找到哪个条件为真 [英] How to find which condition is true without using if statement

查看:124
本文介绍了如何在不使用if语句的情况下找到哪个条件为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个条件。

我想知道的是哪种情况属实,两者都是真/假,或只有一种是真的。

What I want to know is which condition is true, both are true/false, or only one is true.

问题是如果我使用 if 语句,它会增加代码量,并且看起来不像是一个强大的解决方案。

The problem is if I use if statements, it increases the amount of code and it doesn't appear like a robust solution.

C#中是否有任何条件或循环可以一次检查和告知?

Are there any conditions or loops in C# which can check and tell in one go?

任何人都可以为我提供替代和清洁的解决方案?

Can any one provide me with an alternative and clean solution?

场景:

string  garage = getGarageCost(id)
    string  helmet = getHelmetCost(id)

    if(garage == "")

    {

    string selected =  "dropdown is not selected"
    }

    else if(Convert.ToInt32(garage) >= 1)
    {
    string selected = "dropdown is selected"
    return selected value;
    }

    if(helmet == "")
    {
    string selected =  "dropdown is not selected"
    }
    else if(Convert.ToInt32(helmet) >= 1)
    {
    string selected = "dropdown is selected"
    return selected value;
    }

* 编辑:*

我有2个下拉列表,如果选中则将所选值传递给页面。

I have 2 drop-down lists, which if selected passes the selected value to the page.

我必须检查下拉列表列表,然后传递值,如果没有选择任何内容,那么它应传递一个字符串没有选择。

I have to check the dropdown list and then pass the values if nothing is selected then it should pass a string "nothing Selected".

如果它被选中,那么它应该做一些计算。

If it is selected then it should do some calculation.

问题是我必须做5个if语句,因为有5种可能性

The problem is I have to do 5 if statements because there are 5 possibilities


  1. 一个选定

  2. 一个未选中

  3. 无论如何

  4. 两者都被选中

  5. 两者都没有被选中

  1. one selected
  2. one not selected
  3. either way
  4. both selected
  5. both are not selected


推荐答案

开关声明?

int  garage = getGarageCost(id)
int  helmet = getHelmetCost(id)

switch (garage) {

    case 0: 
        do some alternative work; 
        break;
    default: 
        everything else; 
        break;
}

switch (helmet) {

    case 0: 
        do some alternative work; 
        break;
    default: 
        everything else; 
        break;
}

pass data to view;

这篇关于如何在不使用if语句的情况下找到哪个条件为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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