无特定循环数的c#字符串循环

无特定循环数的c#字符串循环,c#,C#,我对循环代码有问题: using System; using System.Globalization; namespace test { class Program { static void Main(string[] args) { string text = Console.ReadLine(); TextInfo ti = CultureInfo.CurrentCulture.TextIn

我对循环代码有问题:

using System;
using System.Globalization;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = Console.ReadLine();
            TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
            Console.WriteLine(ti.ToTitleCase(text).Replace(" ", string.Empty));

            Console.ReadKey();
        }
    }
}
也许它写得不应该这样,因为我找不到修复它的方法。具体来说,我希望这个程序输入多行未知数字的句子,它删除所有空格,并将每个单词的第一个字母改为大写。例如,输入数据为:

我想骑自行车

但瑞克说滑板更好

输出为:

Iwannaridebycle

但里克斯说滑板更好


这个程序不能有用户界面,所以我想在创建字符串列表的同时创建一个列表,但对我来说,问题仍然是如何循环它。我在C++中找到了一个解决方案,他们使用“while(GETLIN(CIN,text){}”),但我认为它在C语言中不起作用。

< P>循环应该做这个技巧。如果没有更多的行,返回null。
  static void Main(string[] args)
  {
      List<string> converted = new List<string>();

      while (true)   // forever
      {
          string text = Console.ReadLine();

          if (text == null)
          {
              break;    // no more lines available - break out of loop.
          }

          // Convert to capitals
          TextInfo ti = CultureInfo.CurrentCulture.TextInfo;

          string convertedText = ti.ToTitleCase(text).Replace(" ", "");

          converted.Add(convertedText);
      }

      // Now display the converted lines
      foreach (string text in converted)
      {
          Console.WriteLine(text);
      }
  }
static void Main(字符串[]args)
{
列表转换=新列表();
虽然(真实)//永远
{
string text=Console.ReadLine();
if(text==null)
{
break;//没有更多可用的行-断开循环。
}
//转换成大写
TextInfo ti=CultureInfo.CurrentCulture.TextInfo;
字符串convertedText=ti.ToTitleCase(text.Replace(“,”);
converted.Add(convertedText);
}
//现在显示转换后的线
foreach(转换后的字符串文本)
{
控制台写入线(文本);
}
}

我认为这可能是一个合适的解决方案。它将从控制台读取输入,直到用户简单地点击“回车”并向您发送一个空字符串。我不知道有没有更动态的方法来实现您的目标。

这取决于您的输入是什么

如果它是一个
IEnumerable
,那么只需使用
foreach

var lines = ... //some IEnumerable<string>

foreach (var line in lines)
{
    //do your thing
}

只要输入流能够产生一个新行,这将再次循环。

那里没有循环。看到
Main
中的代码了吗?把它放在一个循环中。这就是循环的来源:你。你就是循环的来源。循环人。Loopmeister。Looparooney。但是说真的,在循环c时查找“控制台输入”或者在谷歌上,你会看到很多你可以窃取的代码。虽然循环确实存在于C#中。我想知道OP是否知道循环是什么。还有一个
可怕的问题
,表明你缺乏努力,肯定是投了否决票和投票结束票。我很高兴你发现这个问题值得回答,即使你认为它很愚蠢。我可以写我的问题是如何退出一个循环。我也很高兴有人发布了答案,它不是100%有效,但它帮助了我如何做到。顺便说一句,对不起,我的英语,我努力提高它。
var lines = ... //some IEnumerable<string>

foreach (var line in lines)
{
    //do your thing
}
using (var inputStream = ...// some stream)
using (var reader = new StreamReader(inputStream))
{
     var line = reader.ReadLine();

     while (line != null)
     {
          //do your thing
          line = reader.ReadLine();
     }
}