利用这段代码
public class Cfx{
public static void main(String [] args){
for(int i = 0;i < 5;i++){
for(int j = 0;j < 4;j++){
System.out.print("*");
}
System.out.println();
}
}
}
可以输出
****
****
****
****
****
但是我想实现输出如下图形
****
* *
* *
* *
****
请问是不是用if语句限制i的取值?
请各位大虾给出宝贵意见 先谢谢了
public class Cfx{
public static void main(String [] args){
for(int i = 0;i < 5;i++){
for(int j = 0;j < 4;j++){
System.out.print("*");
}
System.out.println();
}
}
}
可以输出
****
****
****
****
****
但是我想实现输出如下图形
****
* *
* *
* *
****
请问是不是用if语句限制i的取值?
请各位大虾给出宝贵意见 先谢谢了
public static void main(String[] args) {
Test.printRect(4, 3);
Test.printRect(6, 4);
}
//打印矩形:a 长 b 宽
public static void printRect(int a,int b){
for ( int i = 0 ; i < b ; i ++ ){
for ( int j = 0 ; j < a ; j ++){
if ( i == 0 || i == ( b -1 ))
System.out.print("*");
else{
if(j==0 || j==(a-1))
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("-----------------------");
}
}
public static void main(String args[]){
xxx x=new xxx();x.p(7,5);}void p(int width,int height){for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
System.out.print(i>0&&i<height-1&&j>0&&j<width-1?" ":"*");
}
System.out.println("");
}
}}
for(int j = 0;j < 4;j++){
if (i==1 || i==2 || i==3)
if (j==0 || j == 3 )
System.out.print("*");
else
System.out.print(" ");
else
System.out.print("*");
}