如何在 c# 中重复 console.writeline x 次?

我正在尝试重复 console.writeline x 次(在 C# 中)。

x(long 类型)是来自 console.writeline 的用户输入。所以如果 x 是 2,我想要 2 个 console.writelines 跟随它。我尝试使用 Enumerable.Repeat 修复它,但这不起作用。还有一个 while 循环,我从 x 中减去 1 直到它为 0 没有成功。建议?


慕桂英4014372
浏览 186回答 2
2回答

小唯快跑啊

类似的东西?static void Main(string[] args){&nbsp; &nbsp; var input = Console.ReadLine();&nbsp; &nbsp; long loopCounter = 0;&nbsp; &nbsp; long.TryParse(input, out loopCounter);&nbsp; &nbsp; for (int i = 0; i < loopCounter; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("My Message");&nbsp; &nbsp; }&nbsp; &nbsp; Console.ReadKey();}
打开App,查看更多内容
随时随地看视频慕课网APP