请帮助c#中的简单模式程序

时间:2015-09-29 06:04:03

标签: c#

我想打印以下图案: -

                 1
             2   3
         4   5   6
     7   8   9  10
11  12  13  14  15

但我得到以下输出: -

               1
           2   3
       4   5   6
   7   8   9  10
 11  12  13  14  15

以下是代码:

static void Main(string[] args)
        {
            int spacelimit = 13, num = 1, n = 5;
            for(int i = 1; i<=n; i++)
            {
                for (int space = spacelimit; space >= i; space--)
                    {
                        Console.Write(" ");
                    }
                for (int k = 1; k <= i; k++)
                    {
                        Console.Write("{0,3:D} ",num++);
                    }
                spacelimit = spacelimit - 3;
                Console.WriteLine();
            }
            Console.ReadKey();
        }

我对这些空间做错了什么?我无法做到。

3 个答案:

答案 0 :(得分:2)

这样就可以了解

int spacelimit = 13, num = 1, n = 5;
for(int i = 1; i <= n; i++)
{
    for(int space = spacelimit; space >= i; space--)
    {
        Console.Write(" ");
    }
    for(int k = 1; k <= i; k++)
    {
        Console.Write("{0,2:D} ", num++);
    }
    spacelimit = spacelimit - 2;
    Console.WriteLine();
}
Console.ReadKey();

我刚刚将3更改为2,即spacelimit - 2{0,2:D}

是的,更改spacelimit也解决了尾随空格的问题,但此解决方案按预期工作...请查看图像。

enter image description here

答案 1 :(得分:1)

如果我们在for循环中创建空间&gt; = i-3,如下所示,它可以正常工作。请检查。感谢。

        int spacelimit = 13, num = 1, n = 5;
        for (int i = 1; i <= n; i++)
        {
            for (int space = spacelimit; space >= i - 3; space--) // HERE, I MADE i-3
            {
                Console.Write(" ");
            }
            for (int k = 1; k <= i; k++)
            {
                Console.Write("{0,3:D} ", num++);
            }
            spacelimit = spacelimit - 3;
            Console.WriteLine();
        }
        Console.ReadKey();

答案 2 :(得分:1)

16的初始值设置为// Animation private Animation animMove; private Animation animFadein; //Method to preform 2 animation in sequence private void doAnimation() { // load the animation animMove = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move); animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); // start move up animation on brnetwork text imageView.startAnimation(animMove); // set animation listener animMove.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.background); relMain.setBackgroundDrawable(drawable); llInput.startAnimation(animFadein); llInput.setVisibility(View.VISIBLE); tvSignUp.startAnimation(animFadein); tvVideo.startAnimation(animFadein); tvSignUp.setVisibility(View.VISIBLE); tvVideo.setVisibility(View.VISIBLE); } }); // set animation listener animFadein.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } }); } after MOVE animation i have to perform FEDIN animation in your case you can replace FEDIN by your Next Animation.