C#如何在两个数字之间找到几个数字(简单) [英] C# how to find couple numbers between 2 numbers (simple)

查看:415
本文介绍了C#如何在两个数字之间找到几个数字(简单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我在控制台中输入2和9它必须显示两个数字之间的几个数字`2,4,6,8。



我尝试过:



for example i input 2 and 9 in console it must show me couple numbers between that two numbers ` 2,4,6,8.

What I have tried:

int i = 0;
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());
            while (i > a && i < b)
            {
                if (i % 2 == 0)
                {
                    Console.WriteLine(i);
                }
                i += 1;
            }

推荐答案

我会做以下脚本。它更清晰可读。



I would do the following script. It is clearer and readable.

int i = 0;
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
for (i = a;i<b;i++)
{
   if (i % 2 == 0)
    {
         Console.WriteLine(i);
    }
               
}





希望有所帮助。

< br $>
最好的问候,



Carlos



Hope it helps.

best regards,

Carlos


开始我等于两者中的最小值并达到最大值:

Start with i equal to the minimum of the two and go up to the maximum of the pair:
int min = Math.Min(a, b);
int max = Math.Max(a, b);
i = min;
while (i < max)
   {
   ...
   i++;
   }


这篇关于C#如何在两个数字之间找到几个数字(简单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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