简体   繁体   中英

printing out the opposite of a ladder in a loop

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

Try:

    // How many lines to print
    for (int i = 4; i > 0; i--) {
      // How many '#' to print in that line
      for (int j = 0; j < 2 * i - 1; j++) {
         System.out.print("#");
      }
      // To next line
      System.out.print("\n");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM