C# 嵌套循环不完全重复
C# Nested Loop not repeating entirely
我的嵌套循环中某处存在错误,导致程序在将值增加 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();
}
}
}
好的...我相信这更精确,更接近您要找的...
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();
}
}
}
我的嵌套循环中某处存在错误,导致程序在将值增加 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();
}
}
}
好的...我相信这更精确,更接近您要找的...
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();
}
}
}