使用两层嵌套循环,实现行列循环打印输出,形成简单图案。
代码如下:
//JHTP Exercise 4.29:Square of Asterisks
//by pandenghuang@163.com
/**(Square of Asterisks) Write an application that prompts the user to enter the size of the side
of a square, then displays a hollow square of that size made of asterisks. Your program should work
for squares of all side lengths between 1 and 20.*/
import java.util.Scanner;
public class SquareOfAsterisks {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请输入要输出的正方形大小(整数):");
int size=input.nextInt();
int number=1;
int sentinel=1;
while (sentinel<=size){
while (number<=size){
System.out.print("*");
number++;
}
System.out.print("\n");
number=1;
sentinel++;
}
}
}
运行截屏:
请输入要输出的正方形大小(整数):10
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************