Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用while循环找到给定数字的因子_C#_Loops_While Loop_Factors - Fatal编程技术网

C# 如何使用while循环找到给定数字的因子

C# 如何使用while循环找到给定数字的因子,c#,loops,while-loop,factors,C#,Loops,While Loop,Factors,我试图完成本页底部的任务,但它给了我一个无限循环,如果我添加教程的建议,将数字除以因子并将其分配给该数字,它不会打印该数字的所有因子,例如,如果我输入20,它将只打印4,5(当我在Console.Write(candidateFactor)下面添加number=number/candidateFactor时) 我想知道我做错了什么,如果有任何帮助,我将不胜感激 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System

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

我想知道我做错了什么,如果有任何帮助,我将不胜感激

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间循环
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“输入一个数字:”);
int number=int.Parse(Console.ReadLine());
控制台。写入(“因子:”);
while(number>1)//将其转换为while
{
int-candidateFactor=2;
while(候选演员1)
{
Console.WriteLine(“,”);
}
//将数字除以找到的系数,并将其重新分配给数字
//如果数字仍然大于1,则打印逗号
}
//别忘了增加系数!
}
}
Console.ReadLine();
}
}
}
试试这个:

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();
这是不必要的,因为您已经在第一次“while”中对此进行了检查。因此,此时您应该已经知道您正在处理一个大于1的数字

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();
Console.WriteLine(“输入一个数字:”);
int number=int.Parse(Console.ReadLine());
控制台。写入(“因子:”);
如果(数字>1)
{
int-candidateFactor=1;

虽然(candidateFactor您从未重新分配
编号,因此它始终具有相同的值。请检查这一行..../不要忘记递增因子!我将candidateFactor++设置为递增,如果我分配number=number/candidateFactor,它不会返回所有因子。您的问题已经有了很好的答案,但我会注意candidateFactor或者应该从1开始。否则,您没有将1显示为数字的有效因子(它是)。好的,谢谢,现在这更有意义了,您是对的,我应该在检查后递增,并将初始候选因子设置为1
if (number > 1)
{
    Console.WriteLine(", ");

}
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();