使用嵌套循环实现正方形打印(Square of Asterisks)
2016-06-17 16:25
387 查看
使用两层嵌套循环,实现行列循环打印输出,形成简单图案。
代码如下:
运行截屏:
请输入要输出的正方形大小(整数):10
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
代码如下:
//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
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
相关文章推荐
- FastJSON 浅显透析
- java synchronized详解(原文基础改进)
- Android-自定义数据库框架【占坑中】
- flask学习笔记(-模板)
- 解决Yum安装依赖问题
- PB9.0对使用js的encodeuri
- 全沾工程师----如何一个人写完web、Android、iOS、接口、CMS
- 解决加载图片OOM
- Android-自定义网络框架【占坑中】
- 您所不知道的nil/Nil/NULL/NSNull的空与空
- 8天学通MongoDB——第五天 主从复制
- 微信分享自定义内容开发-微信JSSDK
- win7下后台运行BAT
- 第三十九讲 项目5 求出1000以内阿姆斯特朗数
- 2016年GitHub 排名前 100 的安卓、iOS项目简介(收藏)
- Centos6.5更新e1000网卡驱动
- 如何不在xml而在代码中使用Margin这是属性
- c++(补)
- mysql 实现行号的方法——如何获取当前记录所在行号
- 数据库分页查询方法