C#嵌套循环不完全重复

时间:2015-10-01 21:52:26

标签: nested-loops

我的嵌套循环中某处出现错误导致程序在程序将值增加1时不显示值的主要因素分析。有没有人有任何想法我做错了什么?任何帮助将不胜感激。

using System;
using static System.Console;

namespace Primes
{
  class Program
  {
    static void Main(string[] args)
    {
        int minValue = 15,
            maxValue = 21;
        int x = minValue,
            y = 2;

        WriteLine("Prime Factors:");
        WriteLine();

        for (minValue = x; minValue <= maxValue; minValue++)
        {
            Write("\n\t{0}", minValue);
            if(minValue <= maxValue)
                while (x > 2)
                {
                    if (x % y == 0)
                    {
                        Write(" : " + Convert.ToString(y));
                        x = x / y;
                    }
                    else
                        ++y;
            }
        }
        ReadLine();
    }
}

}

1 个答案:

答案 0 :(得分:0)

好的......这更精确,更接近你所寻找的东西我相信......

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

using static System.Console;

namespace Primes
{
    class Program
    {
        static void Main(string[] args)
        {
            int minValue = 15,
                maxValue = 21;


            WriteLine("Prime Factors:");
            WriteLine();

            while (minValue <= maxValue )
            {

                int x = minValue;
                int y = 2;

                Write("\n\t{0}", minValue);

                if (minValue <= maxValue)
                    while (x >= 2)
                    {
                        if (x % y == 0)
                        {
                            Write(" : " + Convert.ToString(y));
                            x = x / y;
                        }
                        else
                            ++y;
                    }
                minValue++;
            }
            ReadLine();
        }
    }
}