C#中的三角形打印数字 [英] Print Numbers In triangle Form in C#
本文介绍了C#中的三角形打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打印这样的数字
1
212
32123
4321234
543212345
4321234
32123
132
1
但是没有进入三角形最后1 ...我的代码是
I want to Print Numbers Like This
1
212
32123
4321234
543212345
4321234
32123
212
1
But Not Getting Under The Triangle Last 1...My Code Is
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = i; j > 1; j--)
{
Console.Write(j +" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write(k+" ");
}
Console.WriteLine();
}
for (int k = 4; k > 1; k--)
{
for (int l = k; l > 1; l--)
{
Console.Write(l+" ");
}
for (int n = 1; n <= k; n++)
{
Console.Write(n+" ");
}
Console.WriteLine();
}
}
推荐答案
引用:
for(int k = 4; k> 1; k - )
for (int k = 4; k > 1; k--)
应该是
Should be
for (int k = 4; k >= 1; k--)
而
循环是此任务的更好选择。
while
loops are a better choice for this task.
int y = 0 ;
while ( y < 5 ) F ( ++y ) ;
while ( y > 1 ) F ( --y ) ;
private static void F ( int x )
{
// int y = 0 ;
// while ( y < x ) System.Console.Write ( ++y ) ;
// while ( y > 1 ) System.Console.Write ( --y ) ;
int y = x + 1 ;
while ( y > 1 ) System.Console.Write ( --y ) ;
while ( y < x ) System.Console.Write ( ++y ) ;
System.Console.WriteLine() ;
}
现在加油!
你有代码打印上半部分:使用forloop在控制台应用程序中进行打印 [ ^ ]所以你要做的就是在底部添加另一组以反转它!
就个人而言,我转换了我必须使用的方法来打印线:将内部两个循环移动到方法中并调用它,传递i
通过。
然后添加另一个循环以反向我
,再次调用它!
Now come on!
You have the code to print the top half: print in console application by using forloop[^] so all you have to do is add another set at the bottom to reverse it!
Personally, I'd convert what I had to use a method that prints the line: move the inner two loops into the method and call it, passingi
through.
Then add another loop to "reverse"i
, and call it again!
这篇关于C#中的三角形打印数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文