在c#中使用while循环,在两个num';s只打印num';那可被第三个数字整除

本文关键字:num 三个 数字 打印 两个 while 循环 | 更新日期: 2023-09-27 18:28:42

我正在处理while循环。我目前正在处理一个问题,要求从名为a、b和c的用户那里获得三个数字。我试图显示a和b之间的所有数字,这些数字除以c。我曾想过使用"if",但没有成功。

    public static void Main (string[] args)
    {
        Console.WriteLine ("enter 3 num");
        int num1 = Convert.ToInt32 (Console.ReadLine ());
        int num2 = Convert.ToInt32 (Console.ReadLine ());
        int num3 = Convert.ToInt32 (Console.ReadLine ());
        if (num1 > num2) {
            while (num2 % num3 == 0) {
                Console.WriteLine (num2);
                num2++;
            }
        } else {
            while (num1 % num3 == 0) {
                Console.WriteLine (num1);
                num1++;
            }
        }

    }

在c#中使用while循环,在两个num';s只打印num';那可被第三个数字整除

您的方法(以及迄今为止的其他答案)正在测试ab之间的每个数字,这非常好。

我在下面尝试的是通过一个简单的计算找到a的第一个因子>=,然后继续将c加到这个数字上,直到我们超过上限b

public static void Main (string[] args)
{
    // Note: This crashes if non numeric characters are entered!
    Console.WriteLine ("Please enter 3 numbers:");
    int num1 = Convert.ToInt32(Console.ReadLine());
    int num2 = Convert.ToInt32(Console.ReadLine());
    int divisor = Convert.ToInt32(Console.ReadLine());
    // Find the lowest and highest in case they are entered in the wrong order
    int lowerNum = Math.Min(num1, num2); 
    int upperNum = Math.Max(num1, num2); 
    // Find the first factor over the lower bound
    // E.g. for a = 10, b = 20, c = 3, we have remainder = 1
    //      = 10 + (3 - 1)
    //      = 12
    int remainder = lowerNum % divisor;
    int factor = (remainder == 0)
      ? lowerNum 
      : lowerNum + (divisor - remainder);
    // Calculate all other factors up to the upper bound by simple addition
    while(factor <= upperNum){
      Console.WriteLine(factor);
      factor += divisor;
    }
}

这种方法的优点:

  • for循环中的测试较少
  • 使用加法(+)而不是模(%)

.NET Fiddle here

另一种方法。。。

static IEnumerable<int> GetDividableNumbers(int a, int b, int c)
{
    int start = Math.Min(a, b);
    int quantity = Math.Abs(a - b);
    return Enumerable.Range(start, quantity).Where(num => num % c == 0);
}
static void Main(string[] args)
{
    foreach (int i in GetDividableNumbers(0, 5, 2))
        Console.WriteLine(i.ToString());
}

如果只想使用while,则使用Enumerator

static void Main(string[] args)
{
    IEnumerable<int> numbers = GetDividableNumbers(10, -3, 3);
    IEnumerator<int> enumerator = numbers.GetEnumerator();
    while (enumerator.MoveNext() == true)
        Console.WriteLine(enumerator.Current);
}

试试这个:

Console.WriteLine("enter 3 num");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int num3 = Convert.ToInt32(Console.ReadLine());
int n = num1;
while (n <= num2)
{
    if (n % num3 == 0)
    {
        Console.WriteLine(n);
    }
    n++;
}

您要做的是打印特定数字的所有除数,给定您有2个数字,并且您想取最小值,因此您可以执行以下操作。

在您的主要功能中,您可以执行以下

Console.WriteLine("enter 3 num");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int num3 = Convert.ToInt32(Console.ReadLine());
int a = Math.Min(num1,num2);
int b = Math.Max(num1,num2);
int num = a;
while(num <= b)
{
   if(num%num3 ==0)
      Console.WriteLine("{0}, {1}/{2}={3}", num, num,num3, num/num3);
   num++;
}

这是的工作演示

num1=45和num2=30以及num3=3 的结果

enter 3 num
45
30
3
30, 30/3=10
33, 33/3=11
36, 36/3=12
39, 39/3=13
42, 42/3=14
45, 45/3=15

假设您已经从用户处获得了num1num2num3

int n1 = Math.Min(num1, num2); //num1 is the small number
int n2 = Math.Max(num1, num2); //num2 is the large number
while(n1 <= n2) {
    if (n1 % num3 == 0)
        Console.WriteLine(n);
     n1++
}