为什么第一行会输出星号?

本文关键字:输出 一行 为什么 | 更新日期: 2023-09-27 18:12:32

    public string PrintRandomShape(int length, int width)
    {
        string output = "";
        for (int rows = 1; rows <= length; rows++)
        {
            if (rows == 1)
            {
                for (int cols = 1; cols <= width; cols++)
                    output += "0";
            }
            else
            {
                for (int cols = 1; cols <= width / 2; cols++)
                    output += " ";
                output += "*";
                output += "'n";
            }
        }
            return output;
//expected output is
000000   *
   *
   *
   *

不知道为什么星号在那里

为什么第一行会输出星号?

在else子句中要做的第一件事是添加一个星号,但您永远不会给它添加新行,最简单的解决方案是在if子句

的循环之后添加新行
for (int cols = 1; cols <= width; cols++)
    output += "0";
output += "'n";

虽然这可能是一个学习使用for循环的作业,但您可以使用其他几种方法来创建只有一个for循环的结构,nl:

using System;
using System.Text;
public class Program
{
    public static void Main()
    {
        Console.WriteLine( CreateRandomShape(10, 5) );
    }
    public static string CreateRandomShape(int width, int height) {
        StringBuilder output = new StringBuilder();
        for (int y = 0; y < height; y++) {
            if (y == 0) {
                output.AppendLine(new String('0', width));
            } else {
                output.AppendLine(new String(' ', width / 2) + "*");
            }
        }
        return output.ToString();
    }
}

这里的值的重复是由string构造函数处理的,接受一个char和它需要重复给定char的次数。StringBuilder为每个输出添加新行(通过使用Environment)。NewLine(类似于'n字符,但特定于操作系统)和output.ToString()然后输出字符串内容

正如Sayse所提到的,你在当前的解决方案中得到第一行的星号,仅仅是因为你没有附加换行符。你可以在你的溶液中这样提取它

public string PrintRandomShape(int length, int width)
{
    string output = "";
    for (int rows = 1; rows <= length; rows++)
    {
        if (rows == 1)
        {
            for (int cols = 1; cols <= width; cols++)
                output += "0";
        }
        else
        {
            for (int cols = 1; cols <= width / 2; cols++)
                output += " ";
            output += "*";
        }
        output += "'n"; // this will always append the new line, in both cases...
    }
    return output;
}

这是因为for循环中缺少NewLine

for (int rows = 1; rows <= length; rows++)
        {
            if (rows == 1)
            {
                for (int cols = 1; cols <= width; cols++)
                    output += "0";
                output += "'n";
            }
            else
            {
                for (int cols = 1; cols <= width / 2; cols++)
                    output += " ";
                output += "*";
                output += "'n";
            }
        }

如果您确实不需要复杂的结构,请尽量避免它们(必须由您调试它们)。在你的特定任务中,你所要做的就是打印出

  • 顶线
  • 体线(length - 2次)

只管去做,请不要把这些实现塞进一个循环:

public string PrintRandomShape(int length, int width) {
  // For public methods validate its arguments 
  if (length <= 0) 
    throw new ArgumentOutOfRangeException("length");
  else if (width <= 0) 
    throw new ArgumentOutOfRangeException("width");
  // When creating a string (esp. in a loop) use StringBuilder
  // length * width + 2 * length - optimization; but it's not that required
  StringBuilder sb = new StringBuilder(length * width + 2 * length);
  // Top Line: width '0''s
  sb.Append('0', width);
  // Body, try avoiding complex loops with conditions
  // length - 2 (-2 : top + bottom lines == 2) lines of
  // '*' + [width - 2] spaces + '*' strings
  for (int i = 0; i < length - 2; ++i) {
    sb.AppendLine();
    sb.Append('*');
    if (width >= 2) {
      sb.Append(' ', width - 2);
      sb.Append('*');
    }
  }
  // Bottom Line width '0''s from new line if total width is greater than 2
  if (length >= 2) {
    sb.AppendLine();
    sb.Append('0', width);
  }
  return sb.ToString();
}

'n是因为它是一个特殊字符而造成的。它在你不想要的地方加了个回车。如果你删除它,程序应该工作。请告诉我:)