简单Javascript圣诞树

简单Javascript圣诞树,javascript,Javascript,我创造了一半的圣诞树,但在这里我被阻止了。有人请帮助我了解如何做左侧也 对于var i=0;i{ 返回i==高度 //那是给树干的 ?“*”.padStartMath.round2*i/2” //实际的树级别 :“*”。重复2*i+1.padStart2*i+height-i'; }。加入“\n”; } document.write`;使用pre元素并在左侧添加空格。我不知道怎么做。我不是一个高级人员。你能给我看看吗?高级?我想你需要阅读一些JavaScript教程。我发现的圣诞树太复杂了,没

我创造了一半的圣诞树,但在这里我被阻止了。有人请帮助我了解如何做左侧也

对于var i=0;i<8;i++{ 对于var j=0;j 关键词是思考

var x=8; 对于设i=0;ii;j-{ 记录、书写; }
对于let k=0;k简单圣诞树函数:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    private static Random RND = new Random(System.currentTimeMillis()); // useful for placing balls
    private static char[] BALLS = {'o','⌾','⛣','⏣','◍'}; // symbols being used as balls

    public static void main (String[] args) throws java.lang.Exception
    {
        int w = 27; // width of the tree
        int b = 10; // number of balls in the tree
        String tree = ""; // this will end up containing the tree

        // build tree
        w = ( w % 2 == 1 ) ? w : 13; // check whether width is odd
        for(int i=1;i<=w;i+=2){
            int s = (w - i) / 2;
            tree += repeat(' ', s) + repeat('*', i) + repeat(' ', s) + "\n";
        }

        // randomly replace some parts by balls
        int i=0;
        while(i < b){
            int j = RND.nextInt(tree.length());
            if(tree.charAt(j) == '*'){
                tree = tree.substring(0, j) + BALLS[RND.nextInt(BALLS.length)] + tree.substring(j+1);
                i++;
            }
        }

        // build trunk
        tree += repeat(' ', (w - 4) / 2) + repeat('*', 4) + "\n" + repeat(' ', (w - 4) / 2) + repeat('*', 4);

        // output
        System.out.println(tree);
    }

    // this function builds a String by repeating a given character a couple of times
    private static String repeat(char c, int l){
        String s = "";
        for(int i=0;i<l;i++)
            s += c;
        return s;
    }
}

我为这个问题编写了以下代码。 我还额外添加了一个漂亮的圣诞树装饰品:-

       ⏣        
      ***       
     *o***    
    **⌾*o**     
   *****⛣**⛣  
  *****⌾****⏣ 
 **◍*◍********
      ****
      ****

上面的答案很大程度上依赖于嵌套循环,虽然我发布了另一种使用现代JS的方法,当然仍然使用单个循环,并将map函数指定给Array.from:

函数xmasheight{ //为后备箱增加1个级别,例如高度+1 返回数组。从{length:height+1},v,i=>{ 返回i==高度 //那是给树干的 ?“*”.padStartMath.round2*i/2” //实际的树级别 :“*”。重复2*i+1.padStart2*i+height-i'; }。加入“\n”; }
document.write`;使用pre元素并在左侧添加空格。我不知道怎么做。我不是一个高级人员。你能给我看看吗?高级?我想你需要阅读一些JavaScript教程。我发现的圣诞树太复杂了,没有解释,这就是为什么我想了解如何创建一个简单的圣诞树。不要使用document.write,您可以使用css将文本集中对齐提示:text align:center。非常感谢@Learn to to Be Transparent。我也不明白。有些人喜欢这样做,而不是回答,或者如果他们不知道答案就忽略。我昨晚看了很多关于如何做的内容,但我说我被阻止了。不知道吗对不起。生活还在继续。我是在你和@Rax Weber的帮助下学会的。谁想变得刻薄我不在乎:。我没看到。谢谢你提供的信息@Rax Weber。欢迎来到!一般来说,如果答案中包括对代码的用途的解释,以及为什么不介绍其他人就能解决问题,那么答案会更有帮助。我很想由于Java!=JavaScript,ed拒绝投票,但结果非常好,我反而对其进行了投票。对于任何人来说,将您的代码移植到他们选择的语言并不难。谢谢,您太好了。Joris,您是一位真正的艺术家,我对您的树印象深刻,即使它不是JS!祝您圣诞快乐!
       ⏣        
      ***       
     *o***    
    **⌾*o**     
   *****⛣**⛣  
  *****⌾****⏣ 
 **◍*◍********
      ****
      ****