for循环输出“*”打印房屋形状

编程入门 行业动态 更新时间:2024-10-11 07:26:03

for循环输出“*”打印房屋<a href=https://www.elefans.com/category/jswz/34/1768223.html style=形状"/>

for循环输出“*”打印房屋形状

/* 用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循环输出“*”打印房屋形状

本文发布于:2024-03-05 09:49:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1711965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形状   房屋

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!