清单上的快速问题 [英] Quick Question on Lists

查看:85
本文介绍了清单上的快速问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EvenandOdds
{
    class Program
    {
        static void Main(string[] args)
        {
            List<double> even = new List<double>();
            List<double> odd = new List<double>();

            Console.Write("odd: ");
            Random randomNums = new Random();

            for (int i = 0; i < 25; i++) ;
            {
                odd.Add(randomNums.Next(0,100));

                Console.WriteLine();

            }
             Console.ReadLine();

            }
        }
    }



我应该在最后一个WriteLine中显示什么以显示奇数?



What do I put in that last WriteLine to show the odd numbers?

推荐答案

好吧,您不必检查数字是否为奇数.为此,请执行以下操作:

Well, you do not check if the number is odd. To do that do something like :

bool isOdd = num % 2 != 0;



在此使用本地变量,然后将随机数分配给该本地变量.然后,您可以使用Console.WriteLine轻松显示它.或使用list[count - 1]访问最后一项.



Use a local variable there and assign the random number to this local. Then you can easily show it using Console.WriteLine. Or access the last item using list[count - 1].


您需要检查奇数,您可以使用 ^ ].
You need to check for odd numbers, which you can do with the % operator[^].
int Num = 5;

if(Num % 2 == 0)
{
  // It's even

}
else
{
  // It's odd

}


这篇关于清单上的快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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