我試圖做一個置換。在這種情況下五,所以5,4,3,2,1。最終,我希望它可以存儲在我的intX類中,最多可以排列100個。計算沒問題,但我想用下面的腳本將輸出的所有單個數相加。C#字符計算問題
所以5! = 5x4x3x2x1 = 120 ----> 1 + 2 + 0 = 3,但低於我的腳本給輸出147:
120
1
2
0
147
我在做什麼錯?我已經嘗試過所有的轉換器,我開始只使用字符串[指針] thingy,我嘗試了不同的數組等等。但它一直持續到147.是否有某種表示形式?
static void Main(string[] args)
{
IntX total=1;
IntX totalsum = 0;
int perm = 5;
for (int i = perm; i > 0; i--)
{
total = total * i;
}
Console.WriteLine(total);
string answerstring = Convert.ToString(total);
char[] answerArray = answerstring.ToArray();
for (int x = 0; x < answerArray.Length; x++)
{
totalsum += Convert.ToInt32(answerArray[x]);
Console.WriteLine(answerArray[x]);
}
Console.WriteLine(totalsum);
}
total = total * i;可以寫成總數* = i; –