0

here is java code that prints triangles one below each other, naming them A, B, C, D; my question is how to print them on*the same level*

public class ex_5_10 {
public static void main(String args[]){
    // (A)
    System.out.println("(A)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column > row) continue ;
            System.out.print("*");
        }
        System.out.println() ;
    }
    //*********************************
    // (B)
    System.out.println("(B)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column < row) continue ;
            System.out.print("*");
        }
        System.out.println() ;
    }
    //********************************
    //(C)
    System.out.println("(C)") ;
    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if( column < row ) System.out.print(" ") ;
            else System.out.print("*");
        }
        System.out.println() ;
    }
    // (D)
    System.out.println("(D)") ;
    for(int row = 0 ; row < 10 ; row++){
        for(int column = 10 ; column >= 0 ; column--){
            if( column > row ){ System.out.print(" ") ; }
            else {System.out.print("*"); }
        }
        System.out.println() ;
    }


}

}

so the java code above will print this:

*
**
***

***
**
*

***
 **
  *

  *
 **
***

now i need to print same figures on the same level!

*    *** ***   *   
**   **   **  **
***  *     * ***

please help me! i m looking for your answer! thanks in advance

Bad Wolf
  • 8,206
  • 4
  • 34
  • 44
ERJAN
  • 23,696
  • 23
  • 72
  • 146

10 Answers10

5

Why don't you want to print like this?

System.out.println("*    *** ***   *") ;
System.out.println("**   **   **  **") ;
System.out.println("***  *     * ***") ;

UPDATE: OK.

If this is homework for loops. Just create matrix (array of arrays) and print by assigned elements of the matrix.

Then print out the matrix line by line.

You should use offsets for each printing cycles. Fox example, for second in pseudo code (not java code!):

//*********************************
// (B)
matrix[0][offset] = "B";

for(int row = 0 ; row < height ; row++){
    for(int column = 0 ; column < width ; column++){
        if(column < row) continue ;
        matrix[row][column+offset] = "*";
    }
}
offset += width + space_length;
Aligus
  • 1,585
  • 1
  • 9
  • 11
3

You could also try somethibng like this:

for(int row = 0 ; row < 10 ; row++){    

    // A
    for(int column = 0 ; column < 10 ; column++){
        if(column > row) continue ;
        System.out.print("*");
    }
    System.out.print("   ");

    // B
    for(int column = 0 ; column < 10 ; column++){
        if(column < row) continue ;
        System.out.print("*");
    }
    System.out.print("   ");


    // C
    for(int column = 0 ; column < 10 ; column++){
        if( column < row ) System.out.print(" ") ;
        else System.out.print("*");
    }
    System.out.print("   ");

    //D
    for(int column = 0 ; column < 10 ; column++){
        if(column > row) continue ;
        System.out.print("*");
    }

    System.out.println() ;
}

Maybe you can simplify it more by changing 4 inner loops to one loop. But it is your homework...

SKi
  • 8,007
  • 2
  • 26
  • 57
2

There's no single-line answer to this.

You need to bear in mind that when you call println(), there will be a newline output at the end of your string, so nothing further will appear on that line.

With that in mind, you'll need to split the logic, so that you assemble the outputs for the various "figures" first, and then once you've processed them, you output the result in a way that makes sense (so the first line of every character, then the second etc.). Instead of calling print while handling each figure, put the results into an appropriate data structure.

For bonus marks, you'll need to word-wrap when enough data is supplied to span the width of the display...

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
2
enter code hereclass y{
        public static void main(String args[])
        {
            for(int x=1;x<=3;x++)
            {
                for(int y=1;y<=x;y++)
                {
                    System.out.print("*");
                }
                for(int sp1=1;sp1<=(4-x);sp1++)
                {
                    System.out.print(" ");
                }
                for(int se=1;se<=(4-x);se++)
                {
                    System.out.print("*");
                }
                for(int sp2=1;sp2<(2*x);sp2++)
                {
                System.out.print(" ");
                }
                for(int p=1;p<x;p++)
                {
                System.out.print(" ");

                }
                for(int stt=1;stt<=(4-x);stt++)
                {
                System.out.print("*");
                }
                for(int spth=1;spth<=(4-x);spth++)
                {
                System.out.print(" ");
                }
                for(int stfr=1;stfr<=x;stfr++)
                {
                System.out.print("*");
                }
                System.out.print("\n");
            }

        }

    }
1

If you want to play with the results you can do the following: (It's a homework)

you can notice that in t he loops there are the following conditions:

  1. if(column < row) continue ; System.out.print("*");
  2. if(column < row) continue ; System.out.print("*");
  3. if( column < row ) System.out.print(" ") ; else System.out.print("*");

  4. if( column > row ){ System.out.print(" ") ; } else {System.out.print("*"); }

Just mix and re-arrange them, and see the out-coming results. And also move the insides of loops for the columns that are internal of one loop for the rows, and add offsets to the columns.
This time you need to use one block of loops and not 4.

Have fun.

GingerHead
  • 8,130
  • 15
  • 59
  • 93
0

You can put the code of all different figures in the same loop and then put calculated spaces between each figures. Find the patterns of the spaces between the figures.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

You would move the internal for loops for the columns inside of one loop for the rows, and add offsets to the columns. The logic for the internal if would then all be column - offset.

Jimi Kimble
  • 504
  • 5
  • 10
0
public class ex_5_10 {
public static void main(String args[]){
    // (A)
    System.out.println("(A)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column > row) continue ;
            System.out.print("*");
        }
        System.out.print() ;
    }
   //*********************************
   // (B)
   System.out.println("(B)") ;

   for(int row = 0 ; row < 10 ; row++){
      for(int column = 0 ; column < 10 ; column++){
         if(column < row) continue ;
         System.out.print("*");
      }
      System.out.print() ;
   }
   //********************************
   //(C)
   System.out.println("(C)") ;
   for(int row = 0 ; row < 10 ; row++){
      for(int column = 0 ; column < 10 ; column++){
          if( column < row ) System.out.print(" ") ;
          else System.out.print("*");
      }
      System.out.print() ;
   }
   // (D)
   System.out.println("(D)") ;
   for(int row = 0 ; row < 10 ; row++){
       for(int column = 10 ; column >= 0 ; column--){
         if( column > row ){ System.out.print(" ") ; }
         else {System.out.print("*"); }
       }
       System.out.print() ;
   }


  }
sabbibJAVA
  • 1,068
  • 1
  • 11
  • 17
0

Code

public class Main {

    public static void main(String[] args) {

    for(int line = 1 ; line<=3; line++){
        for(int patternOne=1; patternOne<=line; patternOne++ ){
            System.out.print("*");
        }

        for(int space=3; space>=line; space--){
            System.out.print(" ");
        }

        for(int patternTwo=3; patternTwo>=line; patternTwo--){
            System.out.print("*");
        }

        for(int space=1; space<=(line*2)-1; space++){
            System.out.print(" ");
        }
        for(int patternThree=3; patternThree>=line; patternThree-- ){
            System.out.print("*");
        }
        for(int space=3; space>=line; space--){
            System.out.print(" ");

        }
        for(int patternFour=1; patternFour<=line; patternFour++){
            System.out.print("*");
        }


        System.out.println();
    }


    }
}

Output

*    *** ***   *   
**   **   **  **
***  *     * ***
ArbitraryChoices
  • 243
  • 1
  • 11
-1

int rows = 10;

    for(int i=0; i<rows ; i++)
    {
        for (int j = 0; j <i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
hitesh141
  • 963
  • 12
  • 25