我想解決一個使用c#的歐拉項目問題(problem 22)。雖然我遇到了問題。可能值得注意的是我對編程相對來說比較陌生,尤其是c#。使用char值與c#中的變量名進行比較?
我需要爲我擁有的字符串集合提供一個單詞評分。這包括總結一個單詞中每個字母的分數,例如, a = 1,b = 2,c = 3等等。爲此,我已將所有26個字母作爲變量與相關分數相關聯。然後我想比較單詞中的每個字母與相同名稱的相關變量。然而,我留下的是char數據類型,我最好的方式是將字符與相關變量進行比較,然後在整數計算中使用變量值。我已經包含了我在下面的代碼,在最後兩行中出現了問題,不包括大括號。 (我有一個快速的看,它似乎不支持在c + +,雖然我不知道c#)。任何幫助將不勝感激。
string[] lines = File.ReadAllLines(@"C:\Users\john\Downloads\names.txt");
//Console.WriteLine(lines[1]);
char[] delimiterChars = { ',', '\t' };
string text = lines[0];
string[] names = text.Split(delimiterChars);
Console.WriteLine("{0} words in text:", names.Length);
Array.Sort(names);
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int h = 8;
int i = 9;
int j = 10;
int k = 11;
int l = 12;
int m = 13;
int n = 14;
int o = 15;
int p = 16;
int q = 17;
int r = 18;
int s = 19;
int t = 20;
int u = 21;
int v = 22;
int w = 23;
int x = 24;
int y = 25;
int z = 26;
int[] nameTotal;
for (int count = 0; count < names.Length; count++)
{
string name = names[count];
int total = 0;
for (int count2 = 0; count2 < name.Length; count2++)
{
nameTotal[count] = name.Substring(count2) + total;
total = total + nameTotal[count];
}
}
'INT letterIndex = CH - 'A';'C#數從0 – 2012-04-22 21:36:23
包括這裏的背景是一個好主意,因爲你的直接問題是誤導。 – CodesInChaos 2012-04-22 21:48:55
看到這行'for(int count2 = 0; count2> name.Length; count2 ++)'。 '>'或'<? – 2012-04-22 21:49:56