我来自C++背景,用它创建基本的2D游戏,但我正在努力自学C#,以便我可以使用它获得最基本的工作。我不在学校,但我正在关注 ProjectEuler.net 的问题。
问题问题被注释到代码中。我无法判断我是否解决了它,因为我无法将数字从列表显示到控制台应用程序中。
我尝试使用Console.WriteLine
直接从变量值写入控制台,但我没有任何运气。我还尝试将所有 int 列表值转换为字符串值并显示它们,但这也没有用。我不是在寻找数字 4 的答案,只是想显示列表,以便我自己找到答案。
为什么我无法获取要写入控制台的列表?
任何帮助不胜感激!
static void Main(string[] args)
{
/* A palindromic number reads the same both ways.
* The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers. */
// 100 x 100 = 10000
// 999 x 999 = 998001
List<int> palindromeContainer = new List<int>();
int Increment = 2;
int Holder = 0;
for (int i = 100; i <= 999; ++i)
{
int j = i;
while (j <= 999)
{
do
{ Holder = i * j; // Gets all Possible Combinations of i * j
if ((Holder % Increment) != 0) // Checks for Prime Numbers
{
++Increment;
}
else if (Increment == Holder - 1 && Holder % Increment != 0 )
{
palindromeContainer.Add(Holder);
Increment = 2;
break;
}
else if (Increment == Holder - 1 && Holder % Increment == 0)
{
Increment = 2;
break;
}
} while (Increment < Holder);
++j;
}
}
palindromeContainer.Sort();
foreach (int line in palindromeContainer)
{
Console.WriteLine(line); // Display all items in list
}
首先注释
掉你的循环逻辑,并在没有:
List<int> palindromeContainer = new List<int>();
palindromeContainer.Add(2);
palindromeContainer.Add(1);
palindromeContainer.Sort();
foreach (int line in palindromeContainer)
{
Console.WriteLine(line); // Display all items in list
}
Console.ReadLine();
这将输出到控制台。然后你就会知道这是有效的,控制台输出不是问题。