金字塔的简单java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21373738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:21:46  来源:igfitidea点击:

Simple java program of pyramid

java

提问by user2849331

I am a beginner in Java programing and I want to print a pyramid ,but due to mistake in coding I am not getting favorable output.

我是 Java 编程的初学者,我想打印一个金字塔,但由于编码错误,我没有得到有利的输出。

class p1 {
    public static void main(String agrs[]) {
        System.out.println("The Pattern is");
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (j <= i) {
                    System.out.print("  $");
                } else {
                    System.out.print("   ");
                }
            }
            System.out.println();
        }
    }
}

which is showing

这是显示

The Pattern is

  $            
  $  $         
  $  $  $      
  $  $  $  $   
  $  $  $  $  $

but I want to print

但我想打印

The Pattern is


        $            
       $  $         
      $  $  $      
     $  $  $  $   
    $  $  $  $  $ 

采纳答案by Qadir Hussain

This code will print a pyramid of dollars.

此代码将打印美元金字塔。

public static void main(String[] args) {

     for(int i=0;i<5;i++) {
         for(int j=0;j<5-i;j++) {
             System.out.print(" ");
         }
        for(int k=0;k<=i;k++) {
            System.out.print("$ ");
        }
        System.out.println();  
    }

}

OUPUT :

输出:

     $ 
    $ $ 
   $ $ $ 
  $ $ $ $ 
 $ $ $ $ $

回答by Aravin

Try this one

试试这个

 public static void main(String[] args) 
{
     int x=11;
     int y=x/2; // spaces
     int z=1; // *`s

     for(int i=0;i<5;i++)
    {
         for(int j=0;j<y;j++)
        {
             System.out.print(" ");
        }
        for(int k=0;k<z;k++)
        {
            System.out.print("*");
        }

        y=y-1;
        z=z+2;
        System.out.println();  
    }

}

回答by Ruchira Gayan Ranaweera

You can try in this way.

你可以试试这个方法。

   for(int a=5;a>0;a--){
        int b=0;
       for(b=0;b<a;b++){
           System.out.print(" ");
       }
        for (int j=b;j<5;j++){
            System.out.print(" $ ");
        }
        System.out.println("");

    }

Out put

输出

      $ 
     $  $ 
    $  $  $ 
   $  $  $  $ 

回答by learner

enter image description here

在此处输入图片说明

 import java.util.Scanner;
    public class Print {
        public static void main(String[] args) {
            int row,temp,c,n;
            Scanner s=new Scanner(System.in);
            n=s.nextInt();
            temp = n;
            for ( row = 1 ; row <= n ; row++ )
               {
                  for ( c = 1 ; c < temp ; c++ )
                    System.out.print(" ");

                  temp--;

                  for ( c = 1 ; c <= 2*row - 1 ; c++ )
                      System.out.print("*");

                  System.out.println("");
               }
        }

    }

回答by 6ton

A better pyramid can be printed this way:

一个更好的金字塔可以这样打印:

The Pattern is
     $     
    $$$    
   $$$$$   
  $$$$$$$  
 $$$$$$$$$ 
$$$$$$$$$$$
public static void main(String agrs[]) {
    System.out.println("The Pattern is");
    int size = 11; //use only odd numbers here
    for (int i = 1; i <= size; i=i+2) {
        int spaceCount = (size - i)/2;
        for(int j = 0; j< size; j++) {
            if(j < spaceCount || j >= (size - spaceCount)) {
                System.out.print(" ");
            } else {
                System.out.print("$");
            }
        }
        System.out.println();
    }
}

回答by Shashank Srivastava

public static void showPyramid(int level)
{
    for(int i=0;i<level;i++)
    {
        for(int j=0;j<level-i-1;j++)
        {
            System.out.print(" ");
        }
        for(int k=level-i;k<=level;k++)
        {
            System.out.print("*");
        }
        for(int k=level-i;k<level;k++)
        {
            System.out.print("*");
        }
        for(int j=0;j<level-i;j++)
        {
            System.out.print(" ");
        }

        System.out.print("\n");
    }

}

Output

输出

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








回答by phani

public static void printPyramid(int number) {
    int size = 5;
    for (int k = 1; k <= size; k++) {
        for (int i = (size+2); i > k; i--) {
            System.out.print(" ");
        }
        for (int j = 1; j <= k; j++) {
            System.out.print(" *");
        }
        System.out.println();
    }
}