如何打印对称金字塔? [英] How to print a symmetrical pyramid ?

查看:114
本文介绍了如何打印对称金字塔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Devs



我想打印如下所示的金字塔



 * 
* * *
* * * * *
* * * * * * *
* * * * * * * * *







现在,请考虑代码



  StarPyramid 
{
public static void main( String arr [])
{
int n = 6 ;
for int i = 0 ; i< n; i ++)
{
for int j = n; j> = i; j--)
{
系统。 out .print( );
}

for int k = 0 ; k< i; k ++)System。 out .print( );
// 如果我使用下面提到的循环,那么我得到不对称的金字塔
// for(int m = 0; m< i; m ++)System.out.print(#) ;


系统。 out .println( );
}

}
}





我尝试过:



我得到了不对称的金字塔。请解释我打印对称的逻辑,以及我的逻辑方面有何不同。我做得不好?



提前致谢!!

解决方案

从这里开始:

如何在Java中创建数字金字塔? [ ^ ]

金字塔模式«Java示例说明 [ ^ ]


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较做。

当代码没有达到预期的效果时,你就接近一个bug。



建议:故事一张纸,并尝试手工完成,你的程序应该使用相同的程序。

制作一个包含3列Line#,#spaces和#stars的表并填写

Hello Devs

I want to print a pyramid like shown below

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *




Now , consider the code

class StarPyramid
{
    public static void main(String arr[])
    {
    int n=6;
    for(int i=0;i<n;i++)
    {
        for(int j=n;j>=i;j--)
        {
            System.out.print(" ");
        }
        
        for(int k=0;k<i;k++)System.out.print("#");
        //If i use the loop mentioned below , then i get asymmetrical pyramid
        //for(int m=0;m<i;m++)System.out.print("#");        
        
        
        System.out.println(" ");
    }
        
    }
}



What I have tried:

I get asymmetrical pyramid with this. Please explain me the logic to print a symmetrical one and how is mine different in terms of logic. What i am not doing properly ?

Thanks in advance !!

解决方案

Start here:
How To Create Pyramid Of Numbers In Java?[^]
Pyramid Pattern « Java Examples Explained[^]


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.
Make a table with 3 columns "Line #", "#spaces" and "# stars" and fill it


这篇关于如何打印对称金字塔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆