C# 用C语言制作星形金字塔#
所以我试着用C#做一个金字塔,但我无法让它正确打印。 我这样做是为了我学校的C#班,我真的不知道如何正确地获得金字塔中的空间。我觉得我真的做错了什么 我得到的不是三角形,而是这样的东西:C# 用C语言制作星形金字塔#,c#,C#,所以我试着用C#做一个金字塔,但我无法让它正确打印。 我这样做是为了我学校的C#班,我真的不知道如何正确地获得金字塔中的空间。我觉得我真的做错了什么 我得到的不是三角形,而是这样的东西: * ** *** **** ***** ****** ******* ******** 这是我的密码: using System; namespace Pyramidi { class Ohjelma { static void Main()
*
**
***
****
*****
******
*******
********
这是我的密码:
using System;
namespace Pyramidi
{
class Ohjelma
{
static void Main()
{
int korkeusMax = 0;
int valit = 0;
do
{
Console.Write("Anna korkeus: ");
korkeusMax = Convert.ToInt32(Console.ReadLine());
if (korkeusMax > 0) {
break;
}
else {
continue;
}
}
while(true);
for (int korkeus = 1; korkeus <= korkeusMax; korkeus++)
{
valit = (korkeusMax - korkeus) / 2;
for (int i = 0; i < valit; i++)
{
Console.Write(" ");
}
for (int leveys = 1; leveys <= korkeus; leveys++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
使用系统;
名称空间金字塔
{
Ohjelma类
{
静态void Main()
{
int korkeusMax=0;
int-valit=0;
做
{
控制台。写(“安娜·科克尤斯:”);
korkeusMax=Convert.ToInt32(Console.ReadLine());
如果(korkeusMax>0){
打破
}
否则{
继续;
}
}
虽然(正确);
for(int korkeus=1;korkeus上半场你做得对,但下半场你想做的是另一个for循环,它做的正好相反
因此,您可能希望复制for循环并从
for (int korkeus = 1; korkeus <= korkeusMax; korkeus++)
或者是类似的东西试试这个:
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
for (int i = 0; i < (korkeusMax - korkeus - 1); i++)
{
Console.Write(" ");
}
for (int i = 0; i < (korkeus * 2 + 1); i++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
Console.Write(new String(' ', korkeusMax - korkeus - 1));
Console.Write(new String('*', korkeus * 2 + 1));
Console.WriteLine();
}
是的,对不起,我打字太快了,你很难理解你的想法,你唯一能做的就是在偶数线上的星星之间添加空格(或者让它们左对齐)我知道,但我不确定是什么原因导致星星看起来像那样。编辑:如果我想继续拥有金字塔,我会怎么做?左对齐不是一个选项。这似乎可行。遗憾的是,这项学校作业必须使用for()循环,所以我们实际上要学习使用它们(“不幸的是,这项学校作业必须使用for()循环来完成,所以我们实际上学会了使用它们。”我知道的没错。不管怎样,我用for循环来完成它,用while循环也一样。
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
Console.Write(new String(' ', korkeusMax - korkeus - 1));
Console.Write(new String('*', korkeus * 2 + 1));
Console.WriteLine();
}