如何为此问题提供条件 [英] How to give condition for this problem

查看:117
本文介绍了如何为此问题提供条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在编写代码

Hi,

I am working on a code

int a = 0, b = 1, c = 2, d = 3;

                    if (select1 == "Red")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[a]);
                    if (select1 == "Yellow")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[b]);
                    if (select1 == "Green")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[c]);
                    if (select1 == "Orange")
                        busOneDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[d]);



                    if (select2 == "Red")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[a]);
                    if (select2 == "Yellow")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[b]);
                    if (select2 == "Green")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[c]);
                    if (select2 == "Orange")
                        busTwoDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[d]);




用户可以选择红色,黄色,绿色,橙色4种颜色中的任意一种

我需要以某种方式编写代码,如果用户选择红色和黄色,那么我需要选择其余两种颜色并对其进行求解.

假设如果用户选择红色和黄色,那么我需要
SCANLA.Run.BusDetails [c],SCANLA.Run.BusDetails [d]并对其进行处理

如何编写代码以忽略用户选择的颜色??


谢谢
John




User may select any of the 4 colors red,yellow,green,orange

I need to write a code in a way, if user selcts red and yellow then I need to take remaining two colors and work out on them.

suppose if the user selects Red and Yellow then I need to take
SCANLA.Run.BusDetails[c],SCANLA.Run.BusDetails[d] and work on them

How can I write code to disregard the user selected colors???


Thanks
John

推荐答案

string[] userInput = {"red", "orange"};
string[] colors = {"red", "yellow", "green", "orange"};
for (int i = 0; i < colors.Length; i++)
{
  if (userInput.Contains(colors[i]) == false)
  {
   var busDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[i]);
   // post processing of bus details here (for example, adding busDetails to list of details
  }
}


for (int i = 0; i < 3; i++)
{
  if (msgDetailsOfBus(SCANLA.Run.BusDetails[i] != busOneDetails  & msgDetailsOfBus(SCANLA.Run.BusDetails[i] != busTwoDetails)
  {
   BusDetails busDetails = msgDetailsOfBus(SCANLA.Run.BusDetails[i]);
   .
   .
   .
  }
}


这里说明了使用Linq返回不在其他List中的对象的List的一般原理.您必须将其调整为适合您的工作:
Here''s an illustration of the general principle of returning a List of objects not in some other List, using Linq. You''ll have to adapt it to your work:
List<Color> clrList = new List<Color>{Color.Red, Color.Yellow, Color.Green, Color.Orange};

private List<Color> selectOtherColors(List<Color> selectedColors)
{
   if(selectedColors.Count != 2) throw new ArgumentException("Must supply two Colors !");

   return clrList.Where((clr, ndx) => ! selectedColors.Contains(colorList[ndx])).ToList();
}

测试:

var useTheseColors = selectOtherColors(new List<Color>{Color.Red, Color.Yellow});

这将返回一个List< Green,Orange>

This will return a List<Green,Orange>


这篇关于如何为此问题提供条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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