在循环中打印出一个阶梯的相反形状

huangapple go评论70阅读模式
英文:

printing out the opposite of a ladder in a loop

问题

以下是翻译好的内容:

我想要打印出这个图案,但是我在使用while循环或for循环时遇到了问题。

#######
#####
###
#

但是我的带有while循环的代码打印出了这个图案:

#
##
###
####
#####

我不知道如何将它反向打印出来,有人可以帮帮我吗?

public class oppositeloop {
    public void oppositelad() {
        int g = 1;
        char f = '#';

        while (g <= 5) {
            int r = 1;
            while (r <= g) {
                System.out.print(f);
                r += 1;
            }
            g += 1;
            System.out.println();
        }
    }

    public static void main(String[] args) {
        oppositeloop n = new oppositeloop();
        n.oppositelad();
    }
}
英文:

I want to print out this but I am having trouble with it using while loops or for loop.

#######
#####
###
#

But my code with the while loop prints out this

#
##
###
####
#####

I don't know how to print it backwards could someone help me.



public class oppositeloop
{
    public void oppositelad(){

     int g = 1;
     
     char f = &#39;#&#39;;
     
    while (g &lt;= 5){
        int r= 1;
        while(r &lt;= g){
            System.out.print(f);
            r += 1;
        }
        g += 1;
        System.out.println();
    }
    }

    public static void main(String[] args) {
        oppositeloop n = new oppositeloop();
        
        n.oppositelad();
        
    }
    
}

答案1

得分: 0

尝试:

    // 要打印多少行
    for (int i = 4; i > 0; i--) {
      // 在该行中要打印多少个'#'
      for (int j = 0; j < 2 * i - 1; j++) {
         System.out.print("#");
      }
      // 到下一行
      System.out.print("\n");
    }
英文:

Try:

    // How many lines to print
    for (int i = 4; i &gt; 0; i--) {
      // How many &#39;#&#39; to print in that line
      for (int j = 0; j &lt; 2 * i - 1; j++) {
         System.out.print(&quot;#&quot;);
      }
      // To next line
      System.out.print(&quot;\n&quot;);
    }

huangapple
  • 本文由 发表于 2020年10月5日 01:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64197749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定