简体   繁体   English

如果您有多个具有不同值的变量,并且我想将这些值通过处理该值的代码块传递,我该怎么做?

[英]If you have multiple variables with different values and i want to pass these values through a block of code that process the value,how do i do that?

This is a hypothetical scenario, but lets say that 这是一个假设的场景,但可以说

int age1 = 12, age2=3, age3=7

I want to pass each of these variable to through a block of code that tells me whether or not it the age is greater then 10. 我想将每个变量传递给一个代码块,该代码块告诉我年龄是否大于10岁。

Is there a way to do that without loops and writing a whole bunch of if statements. 有没有一种方法可以做到这一点而无需循环并编写大量的if语句。 (If I have to write 1 if statement that is fine, but not multiple ones) (如果我必须写1个if语句很好,但不能多个)

As a generalized solution, you can try params and Linq ( Any ): 作为通用解决方案,您可以尝试使用paramsLinqAny ):

 static bool HasGreaterThan10(params int[] ages) {
   return ages.Any(age => age > 10);
 }

Example

if (HasGreaterThan10(12, 3, 7)) {
  ...
}

If you want to test if all the ages are greater then 10 use All : 如果要测试所有年龄是否都大于10使用All

static bool AllGreaterThan10(params int[] ages) {
   return ages.All(age => age > 10);
}

...

if (AllGreaterThan10(age1, age2, age3, age4)) {
  ...
}

You can declare the array locally and get rid of method if you want: 您可以在本地声明数组,并在需要时放弃方法:

if (new int[] {age1, age2, age3}.Any(age => age > 10)) {
  ...
}

The concept you are looking for is called arrays . 您正在寻找的概念称为数组

And looping over an array is always preferred over writing code that names things a1, a2, a3, ... and that then explicitly deals with all those values. 并且始终优先于遍历数组而不是编写代码来命名事物a1,a2,a3,...,然后显式处理所有这些值。

The point is: maybe you think that those values will do today, but thing is: there is a certain chance that you will have to use 4 values tomorrow. 关键是:也许您认为这些值将在今天使用,但事实是:明天您很有可能必须使用4个值。 And 7 the other week. 还有7个星期。

You can use the "or else" operator ( || ) 您可以使用“或其他”运算符( ||

bool isGreater(int age1,  int age2, int age3) {
  return (age1>10) || (age2>10) || (age3>10);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我有多个具有不同属性值的文件,如何设置复选框的不确定状态? - how do i set Indeterminate state of a checkbox if i have a multiple files that have different attributes values? 我如何通过MVC4中的剃刀传递值 - How do i pass values through razor in mvc4 如何连续两个random给出不同的值? - How do I have two randoms in a row give different values? 如何通过一个单选按钮传递多个值? - How do I pass multiple values with one radio button? 是否可以创建一个字典,其中的值可以具有多种数据类型? (另外我如何遍历一个数组作为我的值之一) - Is it possible to create a dictionary in which values can have multiple data types? (Also how do I iterate through an array as one of my values) 你如何在 C# 中传递多个枚举值? - How do you pass multiple enum values in C#? 我已将表格字段和文本框值存储在两个不同的列表中 <string> 现在我想在插入查询中使用那些列表怎么做? - I have stored Table fields and Textbox values in two different list<string> now I want to use those lists in insert query how to do it? 如果在MainWindow视图上有2个UserControl,并且想在它们之间传递属性,该怎么做? - If I have 2 UserControls on a MainWindow view, and I want to pass a property between them, how do I do it? 我如何 select 数组中的多个值? - How do I select multiple values in an array? 如何将String拆分为多个值? - How do i split a String into multiple values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM