关于c#代码字符串和int

时间:2011-11-25 10:15:38

标签: c#

我希望c#打印像“internet”这样的词:

internet
 internet
  internet
   internet
    internet
     internet
      internet
       internet

这是我的代码:

string word = ("internet");

for (int i = 0; i < word.Length ; i++) 
{
  Console.WriteLine(word);
}

用我的代码我就是那样

internet
internet
internet
internet
internet
internet
internet
internet

5 个答案:

答案 0 :(得分:14)

        string word = "internet";

        for (int i = 0; i < word.Length; i++)
        {
            Console.Write(new string(' ', i));
            Console.WriteLine(word);
        }

答案 1 :(得分:2)

代码应为:

string word = ("internet");

for (int i = 0; i < word.Length ; i++) 
{
    for (int j = 0; j < i ; j++) 
    {
        Console.Write(" ");
    }

    Console.WriteLine(word);
}

答案 2 :(得分:0)

你有没有尝试过:

  string word = ("internet");
  int count = word.length;

  for (int i = 0; i < count ; i++) 
  {
    Console.WriteLine(word);
    word =""+word;
  }

答案 3 :(得分:0)

尝试以下代码;它会起作用:

string word = ("internet");
string displayword = ("internet");

for (int i = 0; i < word.Length; i++)
{
    Console.WriteLine(displayword);
    displayword = " " + displayword;
}

答案 4 :(得分:0)

使用string.PadLeft()填充带有前导空格字符的单词,比目前提出的更短的解决方案:

string word = "internet";
for (int i = 0; i < word.Length; i++)
    Console.WriteLine(word.PadLeft(word.Length + i));