如何输出打印一个空心的矩形 三角形 菱形
能不能解释一下这段代码,有点看不懂,谢谢
public class test {
public static void main(String[] args) {
for(int i=1;i<=5;i++) {
switch (i){
case 1:
case 5:
for(int j=1;j<=5;j++) {
System.out.print("*");
}
break;
default:
for(int k=1;k<=5;k++) {
if(k==1||k==5) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
for(i=1;i<=5;i++){
for(j=1;j<=5;j++){
if(i!=1&&i!=5){
if(j>1&&j<5){
System.out.print(" ");
continue;
}
else{
System.out.print("*"); }
}
else{
System.out.print("*"); }
}
System.out.println();
}
public void printRectangle() { for ( int i = 0; i < 4; i++ ) { for ( int j = 0; j < 5; j++) { System.out.print(f(i,j)? "*":" "); } System.out.println(""); } }
public void printRectangle() { for ( int i = 0; i < 4; i++ ) { for ( int j = 0; j < 5; j++) { System.out.print(f(i,j)? "*":" "); } System.out.println(""); } } /** * 所要打印的图形边的函数三角形 * f(i,j)为打印4*5矩形的函数 * (i,j)为矩形边return true,否则返回false */ private boolean f(int i, int j) { return ( i == 0 || i == 3 || j == 0 || j == 4 ); }
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
if(j>1&&j<i){
System.out.print();
}
else{
System.out.print("*");
}
System.out.println();
}