带有星星和空间的c#循环
我现在正在为这个简单的循环任务绞尽脑汁 基本上,我想要实现的是: 1) 用户输入星型金字塔的长度 2) 制作一个带有for循环的金字塔 它需要看起来像这样: (如果需要5层楼高;第一排是5格1星;第二排是4格2星,依此类推带有星星和空间的c#循环,c#,loops,C#,Loops,我现在正在为这个简单的循环任务绞尽脑汁 基本上,我想要实现的是: 1) 用户输入星型金字塔的长度 2) 制作一个带有for循环的金字塔 它需要看起来像这样: (如果需要5层楼高;第一排是5格1星;第二排是4格2星,依此类推 * ** *** **** (很难格式化,但你明白我的意图。) 我现在有这个 public void Pyramid() { Console.WriteLine("Give the hight of the pyrami
*
**
***
****
(很难格式化,但你明白我的意图。)
我现在有这个
public void Pyramid()
{
Console.WriteLine("Give the hight of the pyramid");
_aantal = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= _aantal; i++) // loop for hight
{
for (int d = _aantal; d > 0; d--) // loop for spaces
{
Console.Write(_spatie);
}
for (int e = 0; e < i; e++) // loop for stars
{
Console.Write(_ster);
}
Console.WriteLine();
}
}
公共无效金字塔()
{
Console.WriteLine(“给出金字塔的高度”);
_aantal=Convert.ToInt32(Console.ReadLine());
for(int i=1;i 0;d--)//空格循环
{
Console.Write(_-spatie);
}
for(int e=0;e
输出始终为插入的空格数,且未正确递减。
虽然如果我调试它,它会正确地倒计时
感谢您的回复。您可以使用string类的构造函数为您创建重复,然后一次打印两个值,这样就不需要额外的for循环了
static void Main(string[] args)
{
int rowHeight = 5;
for (int row = 1; row <= rowHeight; row++)
{
string spaces = new string(' ', rowHeight - row);
string stars = new string('*', row);
Console.WriteLine("{0}{1}", spaces, stars);
}
Console.ReadLine();
}
static void Main(string[] args)
{
int rowHeight = 5;
for (int row = 1; row <= rowHeight; row++)
{
int totalSpaces = rowHeight - row;
for (int j = 0; j < totalSpaces; j++)
{
Console.Write(" ");
}
for (int j = 0; j < row; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
整数行高=5;
对于(int row=1;row,您可以使用string类的构造函数为您创建重复,然后一次打印两个值,这样就不需要额外的for循环了
static void Main(string[] args)
{
int rowHeight = 5;
for (int row = 1; row <= rowHeight; row++)
{
string spaces = new string(' ', rowHeight - row);
string stars = new string('*', row);
Console.WriteLine("{0}{1}", spaces, stars);
}
Console.ReadLine();
}
static void Main(string[] args)
{
int rowHeight = 5;
for (int row = 1; row <= rowHeight; row++)
{
int totalSpaces = rowHeight - row;
for (int j = 0; j < totalSpaces; j++)
{
Console.Write(" ");
}
for (int j = 0; j < row; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
整数行高=5;
对于(int row=1;row那么,您的问题是
for (int d = _aantal; d > 0; d--) // loop for spaces
你真的想要吗
for (int d = _aantal - i ; d > 0; d--) // loop for spaces
但它实际上只是反映了你目前拥有的东西,仍然不能创造出你想要的金字塔外观
我认为在控制台应用程序中,最接近的方法是每隔一行减去一个空格:
for (int d = _aantal-i; d > 0; d-=2) // loop for spaces
这将产生以下输出:
给出金字塔的高度:
十,
你的问题是
for (int d = _aantal; d > 0; d--) // loop for spaces
你真的想要吗
for (int d = _aantal - i ; d > 0; d--) // loop for spaces
但它实际上只是反映了你目前拥有的东西,仍然不能创造出你想要的金字塔外观
我认为在控制台应用程序中,最接近的方法是每隔一行减去一个空格:
for (int d = _aantal-i; d > 0; d-=2) // loop for spaces
这将产生以下输出:
给出金字塔的高度:
十,
明白了
static void Main(string[] args)
{
Console.WriteLine("Give the hight of the pyramid");
string _spatie = " ";
string _ster = "*";
int _aantal = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= _aantal; i++) // loop for height
{
for (int d = i; d < _aantal; d++) // loop for spaces
{
Console.Write(_spatie);
}
for (int e = 1; e <= i; e++) // loop for stars
{
Console.Write(_ster);
}
Console.WriteLine();
}
Console.ReadKey();
}
对称金字塔需要奇数个星星。明白了
static void Main(string[] args)
{
Console.WriteLine("Give the hight of the pyramid");
string _spatie = " ";
string _ster = "*";
int _aantal = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= _aantal; i++) // loop for height
{
for (int d = i; d < _aantal; d++) // loop for spaces
{
Console.Write(_spatie);
}
for (int e = 1; e <= i; e++) // loop for stars
{
Console.Write(_ster);
}
Console.WriteLine();
}
Console.ReadKey();
}
对于对称的金字塔,你需要奇数个星星。我知道你想作为一个控制台应用程序来做这件事,但如果你修改了这段代码,它应该可以正常工作
用Consle.Readline/Write替换textbox1/2
int pyramidstories = int.Parse(TextBox2.Text);
int I = 1;
while (I <= pyramidstories)
{
for (int spacecount = 0; spacecount < (pyramidstories - I); spacecount++)
{
TextBox1.Text += " ";
}
for (int starcount = 1; starcount < I + 1; starcount++)
{
TextBox1.Text += "*";
}
TextBox1.Text += Environment.NewLine;
I++;
}
上面的代码示例显示了一个金字塔,如上图所示我知道您想作为控制台应用程序来执行此操作,但如果您调整此代码,它应该可以正常工作
用Consle.Readline/Write替换textbox1/2
int pyramidstories = int.Parse(TextBox2.Text);
int I = 1;
while (I <= pyramidstories)
{
for (int spacecount = 0; spacecount < (pyramidstories - I); spacecount++)
{
TextBox1.Text += " ";
}
for (int starcount = 1; starcount < I + 1; starcount++)
{
TextBox1.Text += "*";
}
TextBox1.Text += Environment.NewLine;
I++;
}
上面的代码示例显示如上所示的棱锥体,以获得如下棱锥体(具有适当的间距):
您可以使用:
static void Main(string[] args)
{
// The length of the pyramid
int lengte = 18;
// Loop through the length as given by the user
for (int i = 0; i <= lengte; i++)
{
// If its an even number (we don't want 1-2-3.. but 1-3-5.. stars)
if (i % 2 == 0)
{
// Calculate the length of the spaces we need to set
int spatieLengte = (lengte / 2) - (i / 2);
// Display spaces
for (int spaties = 0; spaties <= spatieLengte; spaties++)
{
Console.Write(" ");
}
// Display stars
for (int sterren = 0; sterren <= i; sterren++)
{
Console.Write("*");
}
// Newline
Console.WriteLine();
}
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
//金字塔的长度
整数长度=18;
//按用户给定的长度循环
对于(int i=0;i)要得到这样一个金字塔(具有适当的间距):
您可以使用:
static void Main(string[] args)
{
// The length of the pyramid
int lengte = 18;
// Loop through the length as given by the user
for (int i = 0; i <= lengte; i++)
{
// If its an even number (we don't want 1-2-3.. but 1-3-5.. stars)
if (i % 2 == 0)
{
// Calculate the length of the spaces we need to set
int spatieLengte = (lengte / 2) - (i / 2);
// Display spaces
for (int spaties = 0; spaties <= spatieLengte; spaties++)
{
Console.Write(" ");
}
// Display stars
for (int sterren = 0; sterren <= i; sterren++)
{
Console.Write("*");
}
// Newline
Console.WriteLine();
}
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
//金字塔的长度
整数长度=18;
//按用户给定的长度循环
对于(int i=0;i)您必须将您的“金字塔”视为一个字符矩阵,例如5X5矩阵。然后您需要为矩阵每行中的每一项决定是要插入空格还是星号。我不再多说了。问题是,您永远不会减少总数,所以您将始终保持(高度-1)确实是空格。您应该在计算中引入i
,例如:for(int d=\u aantal-i;d>=0;d--)
你想要一个对称的金字塔吗?因为你实际上是想在那里画一个三角形。你想要一个金字塔还是三角形,在星星之前有空格..如果你想要金字塔,你必须有奇数个星星..!!你不能对称地创建一个有奇数个星星和偶数个星星的金字塔你必须这样做nk将您的“金字塔”表示为字符矩阵,例如5X5矩阵。然后,您需要为矩阵每行中的每一项决定是要插入空格还是星形。我不再多说了。问题是,您从不减少总数,因此您将始终保持(高度-1)确实是空格。您应该在计算中引入i
,例如:for(int d=\u aantal-i;d>=0;d--)
你想要一个对称的金字塔吗?因为你实际上是想在那里画一个三角形。你想要一个金字塔还是三角形,在星星之前有空格..?如果你想要金字塔,你必须有奇数个星星..!!你不能对称地创建一个有奇数个星星有时甚至偶数个星星的金字塔。答案如下这是正确的结果,但OP说这是一个循环练习!!如果解决方案不使用循环,对他来说没有任何用处。第2点简单地说“使用for循环”,而这个答案使用了一个。@boisvert为了语义起见,我使用了一个for循环,但我也用所有循环更新了答案;)答案给出了正确的结果,但OP表示这是一个循环练习!!如果解决方案不使用循环,对他来说没有任何用处。第2点只表示“使用for循环”,而这个答案使用了一个。@boisvert为了语义起见,我使用了一个,但我也用所有循环更新了答案;)是的!是对提供工作答案的现有代码的最小更改是的!是对提供工作答案的现有代码的最小更改对称金字塔实际上需要奇数个星星,但他希望在阶段2个星星和4个spa