【发布时间】:2018-02-23 20:52:20
【问题描述】:
我有以下测试需要通过:
[TestMethod]
public void ShouldRepeatANumberOfTimes()
{
Simon simon = new Simon();
Assert.AreEqual("hello hello hello", simon.Repeat("hello", 3));
//So if parameter 3 was to be exchanged with 7, it would write "hello" seven times in one sentence.
}
对于这个作业,我认为 for 循环将是一个自然的解决方案。所以我尝试了这个:
internal object Repeat(string v1, int v2)
{
for (int i = 0; i < v2; i++)
{
return "hello ";
}
return v1;
}
我收到以下错误:
检测到无法访问的代码。
具体来说,i++ 中的i 下方有一个“错误行”。任何人都能够发现什么是错的?提前致谢。
【问题讨论】:
-
在第一个
return语句之后函数返回... -
您误解了
return的工作原理。
标签: c# object for-loop methods tdd