如果检查则反转

时间:2012-10-26 17:23:33

标签: c# if-statement

我有一个巨大的检查列表,检查例如整数是4还是10,如果是4则将int更改为10,反之亦然所以我的检查将是这样的:

int i = getval();
if (i == 4)
{
    i = 10;
}
else if (i == 10)
{
    i = 4;
}

我的问题是有另一种方法可以做到这一点,而无需检查每个条件。

8 个答案:

答案 0 :(得分:6)

您正在寻找 switch 声明。

int i = getval();
switch(i)       
      {         
         case 4:   
            i = 10;
            break;                  
         case 10:            
            i = 4;
            break;                 
         default:            
            Console.WriteLine("Invalid selection. Please select 4 or 10.");            
            break;      
       }

答案 1 :(得分:6)

如果你有一个巨大的列表,你可能会考虑一些列表结构。

static Dictionary<int, int> exchange = new Dictionary<int, int>();

static Constructor()
{
    AddExchangePair(4, 10);
    AddExchangePair(3, 12);
    ...
}

static void AddExchangePair(int a, int b)
{
    exchange.Add(a,b);
    exchange.Add(b,a);
}

public staic bool Exchange(ref int value)
{
    int newValue = 0;
    bool exchanged = exchange.TryGetValue(value, out newValue);
    if (exchanged) value = newValue;
    return exchanged;
}

这适用于庞大的交换对列表。

如果您使用重复的号码拨打AddExchangePair,例如(7,14)和(14,16)你会得到一个例外。您可能必须考虑在这种情况下该怎么做。

答案 2 :(得分:2)

我不同意使用开关,因为你有一个“巨大的支票清单”。我会使检查自己的类由Dictionary支持。这将有助于最小化switch语句的大小,并强制分离检查和其余代码:

class Cases
{
    private  static readonly Dictionary<int, int>
        List = new Dictionary<int, int>
        {
            {9, 5},
            {3, 2},
            {7, 12},
            {4, 10}
        }; 
    public static int GetCaseValue (int v)
    {
        int result = 0;
        return List.TryGetValue(v, out result) ? result : v;
    }
}
class Program
{
    public static void Main()
    { 
        var test = Cases.GetCaseValue(4);
        test = Cases.GetCaseValue(12);
    }
}

答案 3 :(得分:1)

switch(i)
{
case 4 : i=10; break;
case 10: i=4; break;
}

答案 4 :(得分:1)

你不会绕过某种if / switch语句,因为没有简单的方法可以从4到10再返回。 如果它是0并且你在它之间交换X,你可以去variable = X - variable;交换它就好了,但对于4和10,上面的代码没问题。

答案 5 :(得分:1)

试试这个:

int i = getval() == 4 ? 10 : 4;

应检查getval()是否为4,然后在4到10之间切换。

答案 6 :(得分:1)

这就是你想要的。

int i = getval();
switch (i)
{
    case 4:
    i=10;
    break;
    case 10:
    i=4;
    break;
}

答案 7 :(得分:1)

有人打败了我(并且写得更好),但是自从我写了代码后我就发布了它。

我还要注意,ref这里的使用可能只是为了保持对你的问题的顺从而在我们的答案中,实际上这样的事情可能会使用功能方法,而不是调用{{ 1}}它会调用Swap(ref i),如果发现不匹配,Swap会返回它的输入。当然,你可能需要使用i = Swap(i) - 我无法想到一个明显的问题。

ref

输出:

void Main()
{   
    int i;

    i = 1; 
    Swap(ref i); // no swap
    Console.WriteLine (i);

    i = 10; 
    Swap(ref i); // swap with 4
    Console.WriteLine (i);

    i = 4;
    Swap(ref i); // swap with 10
    Console.WriteLine (i);
}

void Swap(ref int i)
{
    if(swaps == null)
    {
        swaps = new List<Tuple<int, int>>();
        swaps.Add(Tuple.Create(4, 10));
    }

    int compareTo = i;
    var swap1 = from c in swaps where c.Item1 == compareTo select c.Item2;
    var swap2 = from c in swaps where c.Item2 == compareTo select c.Item1;

    if(swap1.Any())
        i = swap1.Single();
    else if(swap2.Any())
        i = swap2.Single();
}

List<Tuple<int, int>> swaps;
相关问题