C# 加载整数并显示奇数/偶数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2366634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:47:46  来源:igfitidea点击:

C# Load integers and display odd / even

c#

提问by oJM86o

Hello wondering if there is an easier way to display odd / even numbers. I know I could do a for loop and load a list. Then I can write another for loop to loop through the list and check if a value is odd / even:

您好,想知道是否有更简单的方法来显示奇数/偶数。我知道我可以做一个 for 循环并加载一个列表。然后我可以编写另一个 for 循环来遍历列表并检查值是否为奇数/偶数:

for(i=0; i<100; i++)
 if(myList[i]%2==0) //even
    //do something
 else
    //odd do something

But is there any way to shorten this up just so that I can easily get a list of odd or even numbers. Not homework just wondering.

但是有什么方法可以缩短它,以便我可以轻松获得奇数或偶数列表。不是作业只是想知道。

采纳答案by JonH

Could you use some sort of lambdas:

你能不能使用某种 lambda 表达式:

//load a list, t, with 100 integers
List<int> t = Enumerable.Range(1, 100).ToList();

//find odd numbers
var oddNumbers = t.Where(num => num%2 != 0);

//find even numbers
var evenNumbers = t.Where(num => num%2 == 0);

//print odd numbers
foreach (int i in oddNumbers)
    Console.WriteLine(i);

//print even numbers
    foreach(int i in evenNumbers)
        Console.WriteLine(i);

The Enumerable just loads the list with 1-100, and then I simply snatch all odds / evens and then print them. This all can be shortened to:

Enumerable 只加载 1-100 的列表,然后我简单地获取所有赔率/偶数,然后打印它们。这一切都可以缩短为:

var e = Enumerable.Range(1, 100).Where(num => num%2==0); //for even numbers
var o = Enumerable.Range(1, 100).Where(num => num%2!=0); //for odd numbers

e,o have an implicit type var. The compiler can determine its type so these two lines are equivalent to:

e,o 有一个隐式类型 var。编译器可以确定其类型,因此这两行等效于:

List<int> eo = Enumerable.Range(1, 100).ToList(); //must tell it its a list

Then to find the odds / evens directly to a list type:

然后直接找到一个列表类型的赔率/偶数:

List<int> o = eo.Where(num => num%2!=0).ToList();
List<int> e = eo.Where(num => num%2==0).ToList();

And to print it is listed in my initial code.

并打印它列在我的初始代码中。

回答by Jesper Palm

The LINQ way... Odd and Even numbers between 1 and 100.

LINQ 方式... 1 到 100 之间的奇数和偶数。

var even = Enumerable.Range(1,100).Where(i => i % 2 == 0);
var odd = Enumerable.Range(1,100).Where(i => i % 2 != 0);

回答by Reed Copsey

You can use LINQ to pull out just the odd or even, and then process:

您可以使用 LINQ 只提取奇数或偶数,然后处理:

var even = myList.Where(i => i%2==0);
foreach(var number in even)
     // do something

回答by Ian Jacobs

Populate your list according to these formulas

根据这些公式填充您的列表

Odds[0->N] = 2*i+1
Evens[0->N] = 2*i

回答by davecoulter

I don't think you have to create the first loop. You could just:

我认为您不必创建第一个循环。你可以:

for (i=1; i < 101; i++)
{
   if (i % 2 != 0)
   {
      //do something
   }
}

Or even:

甚至:

for (i=1, i < 101, i+=2)
{
   //do something
}

回答by Jason Kleban

If you only need half of the numbers, just generate half the numbers ( .. ; .. ; i = i + 2)

如果您只需要一半的数字,只需生成一半的数字 ( .. ; .. ; i = i + 2)

If you need all the numbers but want to avoid an additional loop, you can mark them or process them during the initial loop.

如果您需要所有数字但又想避免额外的循环,您可以在初始循环中标记或处理它们。

Or, during creation, make up to three lists/arrays - one for all numbers, another for odds, and the third for evens.

或者,在创建过程中,最多可以创建三个列表/数组 - 一个用于所有数字,另一个用于赔率,第三个用于偶数。

Is there a specific application?

有具体的应用吗?

回答by Ashot

var t = Enumerable.Range(1, 100).ToList();
var oddNumbers = t.Where(n => (n & 1) != 0).ToList();
var evenNumbers = t.Where(n => (n & 1) == 0).ToList();