/*
键盘录入1-20数字,打印空心正方形
* * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * *
1,先进行键盘录入变量
2,需要行循环和列循环
3,进行判断是否满足正方形的四条边.
*/
import java.util.Scanner; //导包
class DaDiShu {
public static void main (String[] args){
Scanner sc = new Scanner(System.in); //创建键盘录入对象;
System.out.println("请输入一个整数,范围是1-20之间");
int num = sc.nextInt(); //将键盘录入的整数赋值给num;
for (int i = 1; i < num ;i++ ) { //行循环
for (int j = 1;j < num ;j++ ) { //列循环
if (i == 1 || j == 1 || i == num - 1 || j == num - 1) { //判断是否是正方形的四条边
System.out.print("* "); //满足条件打印*
}else{
System.out.print(" "); //不满足条件打印空格
}
}
System.out.println(); //进行换行
}
}
}
|