easy java code,printing stars

2019-09-04 17:54发布

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

9条回答
疯言疯语
2楼-- · 2019-09-04 18:21

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.

查看更多
做自己的国王
3楼-- · 2019-09-04 18:23

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...

查看更多
做个烂人
4楼-- · 2019-09-04 18:24

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.

查看更多
欢心
5楼-- · 2019-09-04 18:26
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");
            }

        }

    }
查看更多
Ridiculous、
6楼-- · 2019-09-04 18:28
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() ;
   }


  }
查看更多
Ridiculous、
7楼-- · 2019-09-04 18:33

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...

查看更多
登录 后发表回答