您的位置:首页 > 其它

使用嵌套循环实现正方形打印(Square of Asterisks)

2016-06-17 16:25 387 查看
使用两层嵌套循环,实现行列循环打印输出,形成简单图案。

代码如下:

//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

**************************************************

**************************************************

**************************************************

**************************************************

**************************************************

**************************************************

**************************************************

**************************************************

**************************************************

**************************************************



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: