如何减少IF语句的数量

huangapple go评论73阅读模式
英文:

How can I reduce the no of IFs

问题

如何减少IF语句的数量并有效地重写相同的内容。

名称是动态的。名称可以是"一百"或"一千"。它也可以是"二百"或"二千"。我希望如果名称包含单词'one'或'two',它会进入相应的IF条件。

ID和名称包含相同的字符串。ID也将包含"一百"或"一千"...

var numberWords = new List<string> { "One", "Two", "Three" }; // Add more numbers as needed

foreach (var numberWord in numberWords)
{
    if (Name.Contains(numberWord))
    {
        var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(numberWord));
        axisIndex = AddAxis(checkIndex, axis);
        break; // Exit the loop once a match is found
    }
}
// Handle the case when there is no match
if (axisIndex == -1)
{
    // ...
}

谢谢。

英文:

How can I reduce the no of IF statements and rewrite the same efficiently.

Name is dynamic. Name could be "One hundred" or "one thousand". It can "Two hundred" or "Two thousand". I want it to enter the respective IF if Name contains the word 'one' or 'two'.

ID and Name contain same string. ID will also hold "One hundred" or "one thousand"...

         if (Name.Contains(&quot;One&quot;))
         {
             var checkIndex = Chart.YAxis.FindIndex(f =&gt; f.Index == index &amp;&amp;  f.Id.Contains(&quot;One&quot;));                  
             axisIndex = AddAxis(checkIndex, axis);               
         }
         else if (Name.Contains(&quot;Two&quot;))
         {
             var checkIndex = Chart.YAxis.FindIndex(f =&gt; f.Index == index &amp;&amp; f.Id.Contains(&quot;Two&quot;));
             axisIndex = AddAxis(checkAxisIndex, axis);
         }
         else if (Name.Contains(&quot;Three&quot;))
         {
             var checkIndex = Chart.YAxis.FindIndex(f =&gt; f.Index == index &amp;&amp; f.Id.Contains(&quot;Three&quot;));
             axisIndex = AddAxis(checkAxisIndex, axis);
         }
         .....
         else     
         {
            ....
         }

Thank you.

答案1

得分: 3

你可以使用这样的方法:

var possibleNames = new[] { "One", "Two", "Three", "Four" };
var selectedName = possibleNames.FirstOrDefault(n => Name.Contains(n));

if (string.IsNullOrWhiteSpace(selectedName)) // 返回或抛出异常

var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(selectedName));
axisIndex = AddAxis(index, axis)
英文:

You can use an approach like this :

var possibleNames = new []{&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;};
var selectedName = possibleNames.FirstOrDefault(n=&gt; Name.Contains(n));

if(string.IsNullOrWhiteSpace(selectedName)) // return or throw

var index = Chart.YAxis.FindIndex(f =&gt; f.Index == index &amp;&amp; f.Id.Contains(selectedName));
axisIndex = AddAxis(index , axis)


</details>



# 答案2
**得分**: 0

```python
我会尝试解析“Name”字段以获取字符串值。然后剩下的就应该很容易了。

    var value Name.Split(' ')[0];
    
    var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(value));
    axisIndex = AddAxis(index , axis);

如果[String.Split][1]无法胜任,[正则表达式][2]也许能够获得你想要的值。

  [1]: https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-7.0
  [2]: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
英文:

I'd try to parse the Name field to get the string value. Then the rest should be easy.

var value Name.Split(&#39; &#39;)[0];

var index = Chart.YAxis.FindIndex(f =&gt; f.Index == index &amp;&amp; f.Id.Contains(value));
axisIndex = AddAxis(index , axis);

If String.Split isn't up to it a regular expression may be able to get the value you want.

huangapple
  • 本文由 发表于 2023年6月13日 02:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定