如何打印这个金字塔图案? [英] How to print this pyramid pattern?

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

问题描述

我是java的新手。我不知道如何打印这个金字塔图案...

I am new to java. I have no idea how to print this pyramid pattern...

这是我的尝试:

for (int i=0;i<=input;i++) {
   for (int j=1;j<=i;j++) {
       System.out.print("x");
   }
   System.out.println();
}

输入= 8时的预期输出:

The expected output when the input = 8:

       x
      xox
     xoxox
    xoxoxox
   xoxoxoxox
  xoxoxoxoxox
 xoxoxoxoxoxox
xoxoxoxoxoxoxox


推荐答案

您需要在打印前打印空间符号。此外,您可以使用 if(k%2 == 0)来打印两种符号。

You need to print space before you print the symbols. Also, you can use if (k%2 == 0) to print two kinds of symbols.

for (int i=0; i<=input; i++) {
    for(int j=input; j>=i; j--) {
        System.out.print(" ");
    }

    for (int k=1; k<=i*2-1; k++) {
        if (k%2 == 0)
            System.out.print("o");
        else
            System.out.print("x");
    }

    System.out.println();
}

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

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