如何使用while循环查找给定数字的因子

时间:2017-02-20 13:35:10

标签: c# loops while-loop factors

我正在尝试完成本页底部的任务,但它给了我一个无限循环,如果我添加教程的建议将数字除以因子并将其分配给它不打印的数字这个数字的因素,例如,如果我输入20,它将只打印4,5(当我添加number = number / candidateFactor时,我之前在Console.Write(candidateFactor)下添加;

我想知道我做错了什么,任何帮助都会受到赞赏。

https://www.microsoft.com/net/tutorials/csharp/getting-started/looping-logical-expression

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

namespace Loops
{
class Program
{
    static void Main(string[] args)
    {
                Console.WriteLine("Enter a number:");
                int number = int.Parse(Console.ReadLine());
                Console.Write("Factors: ");
                while (number > 1) // convert this to while
                {
                    int candidateFactor = 2;

                    while (candidateFactor <= number) // convert this to while
                    {
                        candidateFactor++;
                        if (number % candidateFactor == 0) // found a factor
                        {

                            Console.Write(candidateFactor);
                            if (number > 1)
                            {
                                Console.WriteLine(", ");

                            }
                    // divide number by the factor you found and assign this back to number
                    // print a comma if number is still greater than 1
                }
                // don't forget to increment factor!
            }
                }
                Console.ReadLine();
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

试试这个:

Console.WriteLine("Enter a number:");
int number = int.Parse(Console.ReadLine());
Console.Write("Factors: ");
if (number > 1) 
{
    int candidateFactor = 2;

    while (candidateFactor <= number)
    {

        if (number % candidateFactor == 0)
        {

            Console.Write(candidateFactor);
            if (candidateFactor != number)
            Console.Write(", ");
            // divide number by the factor you found and assign this back to number
            // print a comma if number is still greater than 1
        }
        // don't forget to increment factor!
        candidateFactor++;
    }
}
Console.ReadLine();

您不需要此循环,可以将其更改为if语句:

while (number > 1) // convert this to while
{...}

这是没有必要的,因为您已经在第一次&#39;中检查了这一点。因此,此时您应该已经知道您正在处理的数字大于一。

if (number > 1)
{
    Console.WriteLine(", ");

}

在每个成功的候选人之后,可以以稍微不同的方式添加数字之间的分隔符。我假设您不希望在最后一个号码后面找到一个,这就是为什么会引入此项检查if (candidateFactor != number)

此外,您应该在进行检查后增加候选人,除非您有充分的理由在每次迭代开始时增加它。

e.g。如果你的候选人将从1开始

Console.WriteLine("Enter a number:");
int number = int.Parse(Console.ReadLine());
Console.Write("Factors: ");
if (number > 1) 
{
    int candidateFactor = 1;

    while (candidateFactor <= number)
    {
        candidateFactor++;
        if (number % candidateFactor == 0)
        {

            Console.Write(candidateFactor);
            if (candidateFactor != number)
            Console.Write(", ");
            // divide number by the factor you found and assign this back to number
            // print a comma if number is still greater than 1
    }
        // don't forget to increment factor!

    }
}
Console.ReadLine();