如何使用Linq打印备用偶数和奇数? [英] How to Print alternate Even And Odd Number using Linq?

查看:96
本文介绍了如何使用Linq打印备用偶数和奇数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Linq打印备用偶数和奇数?

How to Print alternate Even And Odd Number using Linq?

推荐答案

那么你首先要分析问题,将其分解为简单的步骤,然后转向那些步入代码。与您在如何在给定范围内打印备用素数? [ ^ ]。
Well you would start by analysing the problem, breaking it down into simple steps and then turning those steps into code. Much like the advice you have already received at How to print alternate prime number upto given range ?[^].


Console.WriteLine(string.Join("\n", Enumerable.Range(0, 1000)));

是这个LINQ'ish够吗? ;-)

干杯

Andi

PS:或者如果你只想要偶数,你可以添加其中(n =>(n& 1)== 0)范围(...)调用。

Is this "LINQ'ish" enough? ;-)
Cheers
Andi
PS: Or if you want only the even numbers, you may add a Where(n => (n & 1) == 0) to the Range(...) call.


这可能会有所帮助..

This may help..
class Program
    {
        static void Main(string[] args)
        {
            List<int> listint = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            var listodd = listint.Where(x => x % 2 != 0).ToList();
            var listeven = listint.Where(x => x % 2 == 0).ToList();

            Console.WriteLine("---Printing odd numbers----\n");
            string cssodd = string.Join(",", listodd);
            Console.Write(cssodd);
            
            Console.WriteLine("\n\n---Printing even numbers----\n");
            string csseven = string.Join(",", listeven);
            Console.Write(csseven);
            Console.ReadKey();

        }
    }



谢谢


Thanks


这篇关于如何使用Linq打印备用偶数和奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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