Java 如何制作一棵线条为1、3、5、3、5、7、5、7、9、7、9的圣诞树

Java 如何制作一棵线条为1、3、5、3、5、7、5、7、9、7、9的圣诞树,java,Java,如何制作一棵线条为1、3、5、3、5、7、5、7、9、7、9的圣诞树 圣诞树应该是这样的: x xxx xxxxx xxx xxxxx xxxxxxx xxxxx xxxxxxx xxxxxxxxx xxxxxxx xx

如何制作一棵线条为1、3、5、3、5、7、5、7、9、7、9的圣诞树

圣诞树应该是这样的:

               x
              xxx
             xxxxx
              xxx
             xxxxx
            xxxxxxx
             xxxxx
            xxxxxxx
           xxxxxxxxx
            xxxxxxx
           xxxxxxxxx
          xxxxxxxxxxx
它要求用户输入行数。到目前为止我已经做到了。早些时候做了一个吡虫啉*

import java.util.Scanner;

public class Pyramide {

    public static void main(String[] args) {

        // les inn antall rader
        System.out.println("Hvor mange rader skal pyramiden ha?");

        int antallRader;
        Scanner tastatur = new Scanner(System.in);
        antallRader = tastatur.nextInt();

        // skrive ut en kolonne som har saa mange rader som det ble angitt
        // for hver verdi som rad kan faa naar rad starter paa 1, er mindre
        // eller lik antallRader, og oekes hver gang med 1)

        // for hver verdi mellom startverdien og sluttverdien med avstand 1
        // gjenta
        for (int rad = 1; rad <= antallRader; rad++) {
            for (int antallTomt = 1; antallTomt <= antallRader - rad; antallTomt++) {
                System.out.print(" ");
            }
            for (int antallX = 1; antallX <= 2 * rad - 1; antallX++) {
                System.out.print("x");
            }
            for (int antallX = 1; antallX <= 2 * rad - 1; antallX++) {
                System.out.print("x");
            }
            System.out.println();
        }

    }

}
import java.util.Scanner;
公共类金字塔{
公共静态void main(字符串[]args){
//安塔尔雷德莱斯酒店
System.out.println(“管理雷达斯卡尔金字塔哈?”);
国际反辐射雷达;
Scanner tastatur=新扫描仪(System.in);
antallRader=tastatur.nextInt();
//在可检测的范围内,可测量的雷达信号
//对于hver verdi som rad kan faa naar rad starter paa 1,er mindre
//eller lik antallRader,og oekes hver gang med 1)
//对于verdi mellom startverdien og sluttverdien med avstand 1
//吉恩塔

对于(int rad=1;rad来说,第一步可能是抽象内容。我的意思是,制作一个只打印此树的一行的方法:

private static void branch(int width,int centerPoint) {
    for (int i = 0; i < centerPoint - width / 2; i++) {
        System.out.print(' ');
    }
    for (int i = 0; i < width; i++) {
        System.out.print('x');
    }
}
private静态无效分支(int-width,int-centerPoint){
对于(int i=0;i
然后,您可以制作任意形状的树:

public static void main(String... args) {
    System.out.println("Hvor mange rader skal pyramiden ha?");
    Scanner scanner = new Scanner(System.in);
    int numRows = scanner.nextInt();
    int width = 1;
    for (int i = 0;i < numRows;i++) {
        branch(width,numRows / 3 + 2);
        if (width % 3 == 2)
            width -= 2; //Reduces the width by 2 if it's line 2, 5, 8, etc.
        else
            width += 2; //Other lines, increases the width by 2.
    }
}
publicstaticvoidmain(字符串…参数){
System.out.println(“管理雷达斯卡尔金字塔哈?”);
扫描仪=新的扫描仪(System.in);
int numRows=scanner.nextInt();
整数宽度=1;
对于(int i=0;i
第一步可能是抽象内容。我的意思是,制作一个只打印此树的一行的方法:

private static void branch(int width,int centerPoint) {
    for (int i = 0; i < centerPoint - width / 2; i++) {
        System.out.print(' ');
    }
    for (int i = 0; i < width; i++) {
        System.out.print('x');
    }
}
private静态无效分支(int-width,int-centerPoint){
对于(int i=0;i
然后,您可以制作任意形状的树:

public static void main(String... args) {
    System.out.println("Hvor mange rader skal pyramiden ha?");
    Scanner scanner = new Scanner(System.in);
    int numRows = scanner.nextInt();
    int width = 1;
    for (int i = 0;i < numRows;i++) {
        branch(width,numRows / 3 + 2);
        if (width % 3 == 2)
            width -= 2; //Reduces the width by 2 if it's line 2, 5, 8, etc.
        else
            width += 2; //Other lines, increases the width by 2.
    }
}
publicstaticvoidmain(字符串…参数){
System.out.println(“管理雷达斯卡尔金字塔哈?”);
扫描仪=新的扫描仪(System.in);
int numRows=scanner.nextInt();
整数宽度=1;
对于(int i=0;i
您非常接近。请考虑:

int linjer = 0;
int radDenneGang = 1;
while (linjer < antallRader) {
    int antallRaderDenneGang;
    if (antallRader - linjer >= 3)
        antallRaderDenneGang = radDenneGang + 2;
    else
        antallRaderDenneGang = radDenneGang + antallRader - linjer - 1;
    for (int rad = radDenneGang; rad <= antallRaderDenneGang; rad++) {
        for (int antallTomt = 1; antallTomt <= antallRader - rad; antallTomt++) {
            System.out.print(" ");
        }
        for (int antallX = 1; antallX <= 2 * rad - 1; antallX++) {
            System.out.print("x");
        }
        // for (int antallX = 1; antallX <= 2 * rad - 1; antallX++) {
        // System.out.print("x");
        // }
        System.out.println();
    }
    linjer += antallRaderDenneGang - radDenneGang + 1;
    radDenneGang++;
}

你们非常接近。请考虑:

int linjer = 0;
int radDenneGang = 1;
while (linjer < antallRader) {
    int antallRaderDenneGang;
    if (antallRader - linjer >= 3)
        antallRaderDenneGang = radDenneGang + 2;
    else
        antallRaderDenneGang = radDenneGang + antallRader - linjer - 1;
    for (int rad = radDenneGang; rad <= antallRaderDenneGang; rad++) {
        for (int antallTomt = 1; antallTomt <= antallRader - rad; antallTomt++) {
            System.out.print(" ");
        }
        for (int antallX = 1; antallX <= 2 * rad - 1; antallX++) {
            System.out.print("x");
        }
        // for (int antallX = 1; antallX <= 2 * rad - 1; antallX++) {
        // System.out.print("x");
        // }
        System.out.println();
    }
    linjer += antallRaderDenneGang - radDenneGang + 1;
    radDenneGang++;
}
给你

String x = "                xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
int ix = x.indexOf('x');
for (int i = 0; i < 12; i++) {
    int j = i/3 + i%3;
    System.out.println(x.substring(j, ix + 2*j + 1));
}
给你

String x = "                xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
int ix = x.indexOf('x');
for (int i = 0; i < 12; i++) {
    int j = i/3 + i%3;
    System.out.println(x.substring(j, ix + 2*j + 1));
}

你的评论是怎么回事?你说得对。一种方法是使用计数器和
%
模数:例如
计数%3
。当结果为0或1时,在
宽度上加+2,当结果为3时,在
宽度上加-2。@rui Poster可能更适合使用非英语的语言?(哇,“。当结果为2时。”)你的评论是怎么回事?你说得对。一种方法是使用计数器和
%
模数:例如
计数%3
。当结果为0或1时,在
宽度上加+2,当结果为3时,在
宽度上加-2。@rui可能更适合使用非英语的语言?(哇,“…当结果为2。”)@user2731202我的丹麦变量名怎么样?;-)互联网的奇迹。再加上描述ASCII艺术是美丽的——即使代码被破坏了,它仍然值得一次投票:——@NicholasRiley,但它是,不是吗?LOL@VeronicaCornejo我不能说我非常赞同丹麦变量名。StackOverflow仅为英语,问题/答案旨在对将来的visito有所帮助在其他语言中,rs和带有变量名的代码更难阅读。是的,我知道您刚刚添加了3(大约)现有名称的名称。@user2731202我的丹麦变量名称如何?;-)互联网的奇迹。再加上描述ASCII艺术是美丽的-即使代码被破坏,它仍然值得一投:-)@NicholasRiley,但它是,不是?LOL@VeronicaCornejo我不能说我很赞成丹麦变量名low仅为英文版,问题/答案旨在帮助未来的访问者,而使用其他语言变量名的代码更难阅读。是的,我知道您刚刚在现有名称中添加了3个(或更多)名称。