需要使用 ToString 方法打印带 * 的三角形

Need to use ToString method to print triangles with *

我想弄清楚如何使用 ToString 方法显示我在嵌套 for 循环中使用 * 符号制作的三角形。我对 ToString 方法有很好的理解,但我不知道如何在 return 值中实际使用 for 循环。

using System;

public class Triangle
{
    public static void Main(string[ ] args)
    {
        Object obj = new Object( );
        Console.WriteLine(obj.ToString( ));
    }

    public override string ToString( )
    {
        for(int row = 1; row <= 10; ++row)
        {
            for(int col = 1; col <= row; ++col)
            {
                Console.Write("*");
            }

            Console.WriteLine( );
        }

        Console.WriteLine( );

        for(int row = 10; row >= 1; --row)
        {
            for(int col = 1; col <= row; ++col)
            {
                Console.Write("*");
            }

            Console.WriteLine( );
        }

        Console.WriteLine( );

        for(int row = 10; row >= 1; --row)
        {
            for(int col = 1; col <= row; ++col)
            {
                Console.Write("*");
            }

            Console.WriteLine( );
        }

        Console.WriteLine( );

        for(int row = 1; row <= 10; ++row)
        {
            for(int col = 1; col <= row; ++col)
            {
                Console.Write("*");
            }

            Console.WriteLine( );
        }
        return x;
        Console.ReadKey( );
    }
}

1- 您需要创建新的 Triangle 对象而不是对象

Triangle tri= new Triagle();
Console.WriteLine(tri.ToString());

2- 您还应该将值存储在某个字符串变量中,return 它,您正在执行控制台打印!

3- 还有您在 return 语句后如何使用 Cosole.ReadKey()。

4- 你的 X 变量是什么 return

5- 有一个简单的解决方案 "might be not efficient but it's simple program anyway"

let's say your row count is 5
string returnVal = "";
for (int i = 0; i < 5; i++) 
  returnVal + ="    *********".Substring(i, 5 + i)+"\n";
return returnVal; 

这可能是作业,所以我不会为您写下实际答案,但我会告诉您您的问题是什么。

  1. 您正在实例化 new Object(),而不是 new Triangle()Object 只是所有 class 继承自的最基本的东西,Triangle 实际上是您自定义的 class。你想用那个。

  2. 您的 Triangle class 应该在其自己的文件中,或者至少与 Program 分开。例如:

class Triangle
{
    //override ToString()
}

class Program
{
    //This is where your main method should be
}
  1. 您的 ToString() 方法需要 return 一个字符串。调用一堆 Console.WriteLine() 与方法应该做的相反。您应该使用 StringBuilder 或至少连接字符串来构建您的巨型三角形字符串,然后 return 从方法中得到它。
using System;
using System.Text;

public class Triangle
{
    StringBuilder sb = new StringBuilder( );

    public static void Main(string[ ] args)
    {
        Triangle tri = new Triangle( );

        Console.WriteLine(tri.ToString());
        Console.ReadKey( );
    }

    public override string ToString( )
    {

        for(int row = 1; row <= 10; ++row)
        {
            for(int col = 1; col <= row; ++col)
            {
                sb.Append("*");
            }
            sb.Append("\n");

        }

        sb.Append("\n");

        for(int row = 10; row >= 1; --row)
        {
            for(int col = 1; col <= row; ++col)
            {
                sb.Append("*");
            }
            sb.Append("\n");
        }

        sb.Append("\n");

        for(int row = 10; row >= 1; row--) // Outer Loop for number of rows
        {
            for(int col = 1; col <= 10 - row; col++)         //Inner loop for number of spaces
            {
                sb.Append(" ");
            }
            for(int k = 1; k <= row; k++)  //Secondary inner loop for number of stars
            {
                sb.Append("*");
            }
            sb.Append("\n");

        }

        sb.Append("\n");

        for(int row = 1; row <= 10; row++)               //Outer Loop for number of rows
        {
            for(int col = 1; col <= 10 - row; col++)         //Inner loop for number of spaces
            {
                sb.Append(" ");
            }
            for(int k = 1; k <= row; k++)  //Secondary inner loop for number of stars
            {
                sb.Append("*");
            }
            sb.Append("\n");
        }
        string s = sb.ToString( );

        return s;


    }

}