【发布时间】:2012-05-03 13:48:13
【问题描述】:
我正在尝试使用 c# (problem 22) 解决 Euler Project 问题之一。虽然我遇到了问题。可能值得注意的是,我对编程比较陌生,尤其是 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 开始计数。 -
在此处包含上下文是个好主意,因为您的直接问题被误导了。
-
见此行
for (int count2 = 0; count2 > name.Length; count2++)。>或<? -
是的,你是对的,应该是
-
也感谢所有快速响应,我没想到会得到如此完整和全面的答案。当我下班磨练时,我会尝试。感谢所有帮助:)