我想解决一个使用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