使用 for 循环创建圣诞树

编程入门 行业动态 更新时间:2024-10-26 21:30:01
本文介绍了使用 for 循环创建圣诞树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 for 循环和嵌套 for 循环制作圣诞树.对于我来说,我需要能够用 *.我已经尝试了无数次,但在制作一个时遇到了问题.这是我的代码:

I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code:

for(int i=1;i<=10;i++){ for(int j=10;j>i;j--){ System.out.println(" "); } for(int k=1;k<=i;k++){ System.out.print("*"); } for(int l=10;l<=1;l++){ for(int h=1;h<=10;h++){ System.out.print(" "); } } System.out.println(); }

我想做的是:

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

推荐答案

试试这个更简单的代码:

Try this much simpler code:

public class ChristmasTree { public static void main(String[] args) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10 - i; j++) System.out.print(" "); for (int k = 0; k < (2 * i + 1); k++) System.out.print("*"); System.out.println(); } } }

它使用了 3 个循环:

It uses 3 loops:

  • 第一个为行数,
  • 第二个用于打印空格,
  • 第三个用于打印星号.

更多推荐

使用 for 循环创建圣诞树

本文发布于:2023-10-19 15:04:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圣诞树

发布评论

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

>www.elefans.com

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