How to move the underline to the left?

    *****-
    ***-
    *-

code:

    public static void main(String[] args) {
        for(int height=0; height<5; height+=2){
            for(int width=5; width-height>0; width--){
                System.out.print("*");
            }
            System.out.println("-");        
        }
    }

Also, I'm trying to make it look like an inverted pyramid for our purpose, using only for loops.

-*****
--***
---*
+4
source share
3 answers

To emphasize the underscore to the left of the pyramid, simply change the value when you exit the dash. Make sure you are still creating a new line after each loop.

Dash example on the left:

public static void main(String[] args) 
{
    for(int height=0; height<5; height+=2)
    {
        System.out.print("-");
        for(int width=5; width-height>0; width--)
        {
            System.out.print("*");
        }
        System.out.println();
    }
}

Here is a method that will turn the pyramid upside down. I changed some of your loops for this, but it is pretty straight forward. Just change the value of the pyramidHeight variable to change the height of the pyramid's output.

An example of an inverted pyramid:

public static void main(String[] args) {

    int pyramidHeight = 3;
    for(int height=0; height<pyramidHeight; height++)
    {
        for(int width=height+1; width>0; width--)
        {
            System.out.print("-");
        }
        for(int width = 1 + (((pyramidHeight-1) - height) * 2); width>0; width--)
        {
            System.out.print("*");
        }
        System.out.println();
    }
}
+1
source

for the first part do it

public static void main(String[] args) {

    for(int height=0;height<5;height+=2) {
        System.out.print("-");
        for(int width=5;width-height>0;width--) {
            System.out.print("*");
        }
        System.out.println();
    }
}

public static void main(String[] args) {

    for(int height=2;height>=0;height--) {
        for(int _under=0;(_under+height)!=3;_under++) {
            System.out.print("-");   
        }
        for(int width=0;width<(2*height+1);width++) {
            System.out.print("*");
        }
        System.out.println();
    }
}
+1
public static void main(String[] args) {

    for(int height=0; height<5; height+=2){
        for(int curHeight= height+1; curHeight > 0; curHeight-=2){
            System.out.print("-"); 
        }

        for(int width=5; width-height>0; width--){
            System.out.print("*");
        }

        System.out.println();
    }
}
+1

Source: https://habr.com/ru/post/1619297/


All Articles