实心
import java.util.Scanner;
public class ChangKuan {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); //使用Scanner类定义对象
System.out.println("please input a int number(Representative length)");
int a = in.nextInt();
Scanner op = new Scanner(System.in); //使用Scanner类定义对象
System.out.println("please input a int number(Representative width)");
int b = in.nextInt();
if (a == 0 && b == 0) {
System.out.println("无法创建长度为0的矩形");
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
空心
import java.util.Scanner;
public class ChangKuan2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); //使用Scanner类定义对象
System.out.println("please input a int number(Representative length)");
int a = in.nextInt();
Scanner op = new Scanner(System.in); //使用Scanner类定义对象
System.out.println("please input a int number(Representative width)");
int b = in.nextInt();
if (a == 0 && b == 0) {
System.out.println("无法创建长度为0的矩形");
}
for (int i=0;i<a;i++) {
for (int j=0;j<b;j++) {
if ( i == 0 ) {
System.out.print("*");
}
else if (i == a-1) {
System.out.print("*");
}
else if (j == 0 || j == b-1) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}