(這不是功課,只是在我使用這本書的練習)需要一個完美的數字運動在C#中一些援助
「的整數被認爲是如果它的因素完全數,其中包括 一個(但不是數字本身),總和爲該數字。例如,6是 的一個完美數字,因爲6 = 1 + 2 + 3.寫法完美 決定參數值是否是一個完美數字。在確定並顯示介於2和1000之間的所有完美數字 的應用程序中使用此方法 。將每個完美數字的因子顯示爲 確認這個數字確實很完美。「
問題是,它顯示的是兩次而不是一次的完美數字。它爲什麼這樣做?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perfect_Numbers2
{
class Program
{
static bool IsItPerfect(int value)
{
int x = 0;
bool IsPerfect = false;
List<int> myList = new List<int>();
for (int i = value; i == value; i++)
{
for (int j = 1; j < i; j++)
{
if (i % j == 0) // if the remainder of i divided by j is zero, then j is a factor of i
{
myList.Add(j); //add j to the list
}
}
x = myList.Sum();
// test if the sum of the factors equals the number itself (in which case it is a perfect number)
if (x == i)
{
IsPerfect = true;
foreach (int z in myList)
{
Console.Write("{0} ",z);
}
Console.WriteLine(". {0} is a perfect number", i);
}
}
return IsPerfect;
}
static void Main(string[] args)
{
bool IsItAPerfectNum = false;
for (int i = 2; i < 1001; i++)
{
IsItAPerfectNum = IsItPerfect(i);
if (IsItPerfect(i) == true)
{
Console.ReadKey(true);
}
}
}
}
}
'for(int i = value; i == value; i ++)'?它的工作原理?巫術! – vroomfondel