简单的金字塔java程序 [英] Simple java program of pyramid

查看:189
本文介绍了简单的金字塔java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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();
        }
    }
}

显示

The Pattern is

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

但我要打印

The Pattern is


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


推荐答案

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

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++) asdas dasdf asdf asd{
             System.out.print(" ");
         }
        for(int k=0;k<=i;k++) {
            System.out.print("$ ");
        }
        System.out.println();  
    }

}

OUPUT:

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

这篇关于简单的金字塔java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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