java打印星型_简单的Java代码,打印星星

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

解决方案

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值