a********r 发帖数: 218 | 1 class MyClass
{
private int numberOfCalls = 0;
private int numberOfLetters = 0;
static void ReverseString(String strIn)
{
char temp;
for (int i = 0; i < strIn.Length; i++)
{
temp = strIn[i];
strIn[i] = strIn[strIn.Length - i];
strIn[strIn.Length - i] = temp;
}
numberOfCalls++;
Console.WriteLine(String.Format("This function has been called {
0}", numberOfCalls));
}
}
望高人指点上面的程序有何flaws |
f*******t 发帖数: 7549 | 2 1.成员变量应该设成static
2.numberOfLetters多余
3.for (int i = 0; i < strIn.Length; i++) 从头做到尾相当于又把string还原了,
应该是i < strln.Length / 2
4.strIn[strIn.Length - i]当i=0时越界,应该是strIn[strIn.Length - i - 1]
5.MyClass和函数都要加public,否则可能无法从其它namespace访问
6.输出函数自带format功能,不需要string.format() |
a********r 发帖数: 218 | |
l******c 发帖数: 2555 | 4 from 0 to n,
swap,
==== doing nothing
no reverse
【在 a********r 的大作中提到】 : class MyClass : { : private int numberOfCalls = 0; : private int numberOfLetters = 0; : static void ReverseString(String strIn) : { : char temp; : for (int i = 0; i < strIn.Length; i++) : { : temp = strIn[i];
|