【问题标题】:How to find the factors of a given number using a while loop如何使用while循环找到给定数字的因数
【发布时间】:2017-02-20 13:35:10
【问题描述】:

我正在尝试完成此页面底部的任务,但它给了我一个无限循环,如果我添加教程的建议将数字除以因子并将其分配给它不会打印所有的数字数字的因素,例如,如果我输入 20,它只会打印 4、5(当我添加之前在 Console.Write(candidateFactor) 下添加的 number = number/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();
            }
        }
    }

【问题讨论】:

  • 您永远不会重新分配number,因此它始终具有相同的值。
  • 检查这一行.... // 不要忘记增加因子!
  • 我把candidateFactor++ 增加,如果我分配number = number/candidateFactor 它不会返回所有的因素。
  • 您的问题已经得到了很好的答案,但我会注意,candidateFactor 应该从 1 开始。否则,您不会将 1 显示为数字的有效因子(它是)。

标签: c# loops while-loop factors


【解决方案1】:

试试这个:

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
{...}

这不是必需的,因为您已经在第一个“while”中检查过了。因此,此时您应该已经知道您正在处理一个大于 1 的数字。

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

}

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

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

例如如果你的候选人从 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();

【讨论】:

  • 好的,谢谢,现在这更有意义了,你说得对,我应该在检查后递增并将初始候选因子设置为 1
猜你喜欢
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2016-06-13
相关资源
最近更新 更多