Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么星号输出在第一行? publicstringprintrandomshape(int-length,int-width) { 字符串输出=”; 对于(int rows=1;rows_C# - Fatal编程技术网

C# 为什么星号输出在第一行? publicstringprintrandomshape(int-length,int-width) { 字符串输出=”; 对于(int rows=1;rows

C# 为什么星号输出在第一行? publicstringprintrandomshape(int-length,int-width) { 字符串输出=”; 对于(int rows=1;rows,c#,C#,来说,在else子句中要做的第一件事是添加一个星号,但是您从来没有给它一个新行,最简单的解决方案是在if子句中的循环之后添加新行 public string PrintRandomShape(int length, int width) { string output = ""; for (int rows = 1; rows <= length; rows++) { if (rows == 1)

来说,在else子句中要做的第一件事是添加一个星号,但是您从来没有给它一个新行,最简单的解决方案是在if子句中的循环之后添加新行

    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   *
   *
   *
   *

for(int cols=1;cols由于它是一个特殊字符,\n导致此问题。它将enter置于您不需要的位置。如果删除它,程序应该可以运行。请告诉我:)

这是因为for循环中缺少换行符

for (int cols = 1; cols <= width; cols++)
    output += "0";
output += "\n";

for(int rows=1;rows虽然这可能是学习使用for循环的一种帮助,但您可以使用其他一些方法来创建此结构,其中只有1个for循环,nl:

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";
            }
        }

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

  • 顶线
  • 正文行(
    长度
    -2倍)
  • 底线
只要这样做,请不要将这些实现塞进一个循环中:

publicstringprintrandomshape(int-length,int-width){
//对于公共方法,验证其参数

if(length)传递什么参数?方法调用中的
length
width
是什么意思?您的输入和意外结果是什么?我猜您想要打印一个矩形?…对于矩形打印,您必须查看最后一行,这可能是因为您没有在第一个输出中添加换行符(我假设第一行中
行==1
不应该有星形?顺便说一句,新字符串('0',长度)将替换你的for循环;)一个你可以找到@Icepickle的例子-有很多方法可以让这段代码更好..我只是想保留我对这个问题的答案,这样对OP来说就不会那么混乱了。没错,我只是想为他喜欢做的事提供框架选项(当然,这可能是循环学习的家庭作业)@Icepickle-请随意添加您自己的答案:)(只需确保它也能回答问题!)
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();
    }
}
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;
}
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();
}