for循环输出“*”打印房屋形状
2017-11-23 21:53
309 查看
/*
用for循环输出"*"打印房屋形状
******
***** *
***** *
***** *
***** *
***** *
****************
* *
* *
* *
* *
****************
*/
public class ForHouse {
public static void main(String[] args) {
// 房顶的行数,行数n可变,房屋大小可随n成比例增大或缩小
int n = 5;
drawHouse(n);
}
public static void drawHouse(int n) {
// up:house上半部
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n; j++) {
if (j == n - i + 1) {
System.out.print("*****");
} else if (j == n + i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 房屋下半部,矩形
// 墙壁 (int) Math.ceil(n * 3 / 5); 向上取整,成比例增高墙壁的高度
int m = (int) Math.ceil(n * 3 / 5);
// m:墙壁高度
for (int i = 1; i <= m + 2; i++) {
//矩形m + 2,,为房屋下半部矩形的总高度
for (int j = 1; j <= n * 2 + 3; j++) {
if (i == 1 || i == m + 2) {
// 矩形上部,下部
System.out.print("*");
} else if (j == 1 || j == n * 2 + 3) {
// 墙壁
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
用for循环输出"*"打印房屋形状
******
***** *
***** *
***** *
***** *
***** *
****************
* *
* *
* *
* *
****************
*/
public class ForHouse {
public static void main(String[] args) {
// 房顶的行数,行数n可变,房屋大小可随n成比例增大或缩小
int n = 5;
drawHouse(n);
}
public static void drawHouse(int n) {
// up:house上半部
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n; j++) {
if (j == n - i + 1) {
System.out.print("*****");
} else if (j == n + i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 房屋下半部,矩形
// 墙壁 (int) Math.ceil(n * 3 / 5); 向上取整,成比例增高墙壁的高度
int m = (int) Math.ceil(n * 3 / 5);
// m:墙壁高度
for (int i = 1; i <= m + 2; i++) {
//矩形m + 2,,为房屋下半部矩形的总高度
for (int j = 1; j <= n * 2 + 3; j++) {
if (i == 1 || i == m + 2) {
// 矩形上部,下部
System.out.print("*");
} else if (j == 1 || j == n * 2 + 3) {
// 墙壁
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
相关文章推荐
- 用for循环打印输出数据三角形图形
- for循环输出树木的形状【java】
- Java中循环问题:for,while,do-while 各种练习(打印上三角、下三角、输出100以内的素数、水仙花、最大公约、最小公倍数、分解质因数、兔子问题)
- for循环输出空心菱形的形状【java】
- for循环输出漏斗的形状【java】
- 使用for循环控制打印输出
- for循环语句应用—打印各种形状
- 用for循环输出星型图案——菱形
- 用for循环打印菱形
- JavaScript基础 使用for循环输出99乘法表(梯形表格)
- java for循环打印等腰三角形
- C语言实现for循环输出图案
- 【程序29】 TestAdd3.java 题目:求一个3*3矩阵对角线元素之和 1.程序分析:利用双重for循环控制输入二维数组, //再将a[i][i]累加后输出。
- 例题:打印乘法口诀。可能大家一看有点难,但只要理解for 循环嵌套,两层循环,外层循环行数,里层循环列数,搞清楚行数和列数之间的关系,就可以轻松做出这道题
- 编程题:利用for循环打印 9*9 表?
- PAT (Advanced Level) 1031. Hello World for U (20) 用字符串输出形状U
- 一个for循环打印二维数组
- 09-使用for循环输出空心菱形(循环)
- ArcGIS API for javascript开发笔记(三)——解决打印输出的中文为乱码问题
- Python(5)使用for...in循环和列表推导式打印一个99乘法表