我无法将4种不同的三角形图案并排显示。这是一个控制台应用程序。
这正是我试图通过使用嵌套for循环来实现的:
* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
*********** ***********
我已经有了各自的模式,但它们当然是一个接一个地出现。
using System;
class Assignment5
{
static void Main()
{
for (int i = 1; i <= 10; i++) // Outer loop for number of rows
{
for (int j = 1; j <= i; j++) // Inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End First Pattern
for (int i = 10; i >= 1; i--) // Outer loop for number of rows
{
for (int j = 1; j <= i; j++) // Inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End Second Pattern
for (int i = 10; i >= 1; i--) // Outer Loop for number of rows
{
for (int j = 1; j <= 10 - i; j++) //Inner loop for number of spaces
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++) //Secondary inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End Third Pattern
for (int i = 1; i <= 10; i++) //Outer Loop for number of rows
{
for (int j = 1; j <= 10 - i; j++) //Inner loop for number of spaces
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++) //Secondary inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End Fourth Pattern
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
} // End class Assignment5
答案 0 :(得分:3)
您没有指定 的实施方式,所以我向您介绍:
private static void Main()
{
Console.WriteLine(
@"* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
*********** ***********");
}
[编辑]好的,这是一个不那么滑稽的答案。 ;)
int n = 10;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j <= i; ++j)
Console.Write("*");
for (int j = 0; j < n-i-1; ++j)
Console.Write(" ");
for (int j = 0; j < n-i; ++j)
Console.Write("*");
for (int j = 0; j < 2*i; ++j)
Console.Write(" ");
for (int j = 0; j < n-i; ++j)
Console.Write("*");
for (int j = 0; j < n-i-1; ++j)
Console.Write(" ");
for (int j = 0; j <= i; ++j)
Console.Write("*");
Console.WriteLine();
}
[第二次编辑]
编写输出n
星号或空格的方法会更具可读性,如下所示:
static void stars(int count)
{
for (int i = 0; i < count; ++i)
Console.Write("*");
}
static void spaces(int count)
{
for (int i = 0; i < count; ++i)
Console.Write(" ");
}
然后:
int n = 10;
for (int i = 0; i < n; ++i)
{
stars(i+1);
spaces(n-i-1);
stars(n-i+1);
spaces(2*i);
stars(n-i);
spaces(n-i-1);
stars(i+1);
Console.WriteLine();
}
答案 1 :(得分:2)
这是你的脑筋急转弯。
for (int n = 10; n > 0; n--)
{
var tri = "".PadRight(11 - n, '*').PadRight(10, ' ') + "".PadRight(n, '*').PadRight(10, ' ');
Console.WriteLine(tri + String.Join("", tri.ToCharArray().Reverse()));
}
输出:
* ******************** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* *********** ***********
答案 2 :(得分:1)
这将为您提供所需的结果。
using System.IO;
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++) // Outer loop for number of rows
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
for (int k = 10; k >= i; k--)
{
Console.Write(" ");
}
for (int l = 10-i; l >= 0; l--)
{
Console.Write("*");
}
for (int k = 0; k <= i*2; k++)
{
Console.Write(" ");
}
for (int k = 10-i; k >= 0; k--)
{
Console.Write("*");
}
for (int k = 10; k >= i; k--)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
Console.Clear();
}
}
答案 3 :(得分:1)
using System;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
int length = 10;
for (int i = 0; i < length; i++)
{
string result = String.Format("{0}{1}{2}{3}",
fillWithStarFromLeft(i + 1, length),
fillWithStarFromLeft(length - i, length),
fillWithStarFromRight(length - i, length),
fillWithStarFromRight(i + 1, length)
);
Console.WriteLine(result);
}
Console.ReadKey();
}
private static object fillWithStarFromRight(int length, int segmentlength)
{
string result = String.Empty;
for (int i = 0; i < length; i++)
{
result += "*";
}
return result.PadLeft(segmentlength, ' ');
}
private static string fillWithStarFromLeft(int length, int segmentlength)
{
string result = String.Empty;
for (int i = 0; i < length; i++)
{
result += "*";
}
return result.PadRight(segmentlength, ' ');
}
}
}
尽管希望它有所帮助
答案 4 :(得分:0)
您正在使用在当前光标位置写入的Console.Write
和Console.WriteLine
命令。你的循环总是在10 * 10字符范围内工作,然后是换行符。
解决问题的方法是绝对光标位置,将光标设置到正确的位置,然后绘制一个字符。每个后续三角形必须移动一个偏移量:
int leftOffset = 0;
// draw first triangle
for x in ...
for y in ...
Console.SetCursorPosition(x, y);
Console.Write("*");
leftOffset += 10;
// draw second triangle
for x in ...
for y in ...
Console.SetCursorPosition(x + leftOffset, y);
Console.Write("*");
.
.
.
请务必在开始前清洁控制台!
答案 5 :(得分:0)
另一个解决方案:你创建一个包含所有&#39; *&#39;的数组。然后你显示它。
我使用了三维数组:行,列,模式编号。
我甚至把一切都转移到0&lt; x&lt; 10,因为在C和C#世界中,数组是基于0的
请注意,我甚至填写了#34;空白&#34;数组的空格。
static void Main()
{
var chars = new char[10, 10, 4];
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= j)
{
chars[i, j, 0] = '*';
}
else
{
chars[i, j, 0] = ' ';
}
}
} // End First Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= 9 - j)
{
chars[i, j, 1] = '*';
}
else
{
chars[i, j, 1] = ' ';
}
}
} // End Second Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= j)
{
chars[i, j, 2] = '*';
}
else
{
chars[i, j, 2] = ' ';
}
}
} // End Third Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= 9 - j)
{
chars[i, j, 3] = '*';
}
else
{
chars[i, j, 3] = ' ';
}
}
} // End Fourth Pattern
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(chars[i, j, k]);
}
}
Console.WriteLine();
}
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
正如我告诉过你的那样,我在数组创建过程中用空格填充了空白空间。我可以跳过它,而是在Console.Write中用chars[i, j, k] == 0
替换' '
的空格,如:
static void Main()
{
var chars = new char[10, 10, 4];
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= j)
{
chars[i, j, 0] = '*';
}
}
} // End First Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= 9 - j)
{
chars[i, j, 1] = '*';
}
}
} // End Second Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= j)
{
chars[i, j, 2] = '*';
}
}
} // End Third Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= 9 - j)
{
chars[i, j, 3] = '*';
}
}
} // End Fourth Pattern
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
{
for (int j = 0; j < 10; j++)
{
if (chars[i, j, k] != 0)
{
Console.Write(chars[i, j, k]);
}
else
{
Console.Write(' ');
}
}
}
Console.WriteLine();
}
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
从那里我们可以删除if
并将其作为for
的一部分,但它变得非常难以理解......
static void Main()
{
var chars = new char[10, 10, 4];
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j <= i; j++)
{
chars[i, j, 0] = '*';
}
} // End First Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10 - i; j++)
{
chars[i, j, 1] = '*';
}
} // End Second Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = i; j < 10; j++)
{
chars[i, j, 2] = '*';
}
} // End Third Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 9 - i; j < 10; j++)
{
chars[i, j, 3] = '*';
}
} // End Fourth Pattern
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
{
for (int j = 0; j < 10; j++)
{
if (chars[i, j, k] != 0)
{
Console.Write(chars[i, j, k]);
}
else
{
Console.Write(' ');
}
}
}
Console.WriteLine();
}
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
答案 6 :(得分:0)
试试这个(非常短的版本)
int sz=10;
for (int i=0;i<sz;i++){
for(int j=0;j<(sz*4);j++){
if(j<=i || (j>=sz && j<=(sz*2)-(i+1))
|| (j>=sz*2 && j>=(sz*2)+i && j<sz*3)
|| (j>=(sz*4)-(i+1))
)
{
Console.Write("*");
}
else {Console.Write(" ");}
}
Console.WriteLine();
}
在此输入(未测试)。但这可能会成功!