使用for循环创建圣诞树

问题描述 投票:0回答:8

我正在尝试使用 for 循环和嵌套 for 循环制作一棵圣诞树。为了做到这一点,我需要能够用 * 制作金字塔。我已经尝试了无数次,但制作时遇到了问题。这是我的代码:

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();  
}

我想做的是:

     *
    ***
   *****
  *******
java for-loop nested-loops
8个回答
17
投票

尝试这个更简单的代码:

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 个循环:

  • 第一个表示行数,
  • 第二个用于打印空格,
  • 第三个用于打印星号。

11
投票

用简单的逻辑就可以做到

for (int i = 0; i < 4; i++) 
            System.out.println("   *******".substring(i, 4 + 2*i));

2
投票
import java.util.Scanner;

public class cmastree{

    public static void main (String[]args){
        Scanner keyboard=new Scanner (System.in);

        int j;
        System.out.println ("Enter a number");
        j=keyboard.nextInt();
        /*take the above part out and change the j variable if you want to set 
        the size*/
        for(int i=1; i<=j; i+=2){
            int numSpaces = (j-i)/2;
        for (int k=0; k<numSpaces; k++){
            System.out.print(" ");
            }
        for(int k=0; k<numSpaces; k++){
            System.out.print("*");
            }
            System.out.println();
        }
    }
}

1
投票
public class ChrismasTree {

    public static void main(String[] args) {

        int sizeOfTree = 9;
        double remainderVal = sizeOfTree % 2 ;
        double ans = sizeOfTree / 2 ;

        if (remainderVal == 0) {
            System.out.println("Invalid number enter 9,19 calculat rest yourself u looser ..");
            System.exit(0);
        }
        int middlePos = (int) Math.round(ans + .5);

        for (int i = 0; i <= sizeOfTree; i++) {
            int lStar = middlePos - i;
            int rStar = middlePos + i;

            if (lStar < 1) {
                break;
            }
            printleaves(lStar, rStar, sizeOfTree);
        }
    }

    public static void printleaves(int a,int b, int size){
        System.out.println();
        for (int i = 1; i <= size; i++) {
            if (i > a && i < b ){
                System.out.print("*");
            }else System.out.print(" ");    
        }   
    }
}

0
投票
public class Stars {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       System.out.println("Enter Row/Column Value::");
        int i,j,k,n;
        n=s.nextInt();
        for(i=1; i<n; i++){
            for(j=n+(n/2); j>i; j--){
                System.out.print(" ");}
            for(k=1; k<=2*i-1; k++){
                System.out.print("*");}
            System.out.println("");
            }
         for(i=1; i<n+(n/2); i++){
            for(j=n+(n/2); j>i; j--){
                System.out.print(" ");}
            for(k=1; k<=2*i-1; k++){
                System.out.print("*");}
            System.out.println("");
        }
          for(i=1; i<n-(n/2); i++){
            for(j=n+(n/2); j>1; j--){
                System.out.print(" ");}
            for(k=n/2; k<=(n/2)+1; k++){
                System.out.print("*");}
            System.out.println("");
        }
    }
}

0
投票
    public class ChristmasTree {
              
    public static void printStars(int number) {
            for (int i = 1; i <= number; i++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    
        public static void printSpaces(int number) {
            for (int i = 0; i < number; i++) {
                System.out.print(" ");
            }
        }
    
        public static void christmasTree(int height) {
            for (int i = 1; i <= height; i++) {
                printSpaces(height - i);
                printStars(i + (i - 1));
            }
         }

 public static void main(String[] args) {
     //     int x = pick some number, but not TOO big )))
          christmasTree(x);
      }
    }

0
投票

你可以尝试这样的事情

    public class ChristmassTree {

public static void main(String[] args) {
    var height = 5; // Adjust the height of the tree
    printChristmasTree(height);
}

public static void printChristmasTree(int height) {
    for (int i=0; i<height; i++){
        printSpaces(height-i-1);
        printAsterisks(i+1);
        System.out.println();
    }
}

public static void printSpaces(int count) {
    for (int i=0; i<count; i++){
        System.out.print(" ");
    }
}

public static void printAsterisks(int count) {
    for (int i=0; i<count; i++){
        System.out.print("* ");
    }
}}

-1
投票
def fist(n)
 k=2*n-2
  for i in range(0,n):
   for j in range(0,k):
   k=k-1
    print(end=" ')
   for j in range(0,i+1):
    print("*",end=" ")
   print()
def second(n)
 k=2*n-2
  for i in range(0,n):
   for j in range(0,k):
   k=k-1
   print(end=" ')
   for j in range(0,i+1):
    print("*",end=" ")
   print()
def stem(m)
 k=11
  for i in range(0,5):
   for j in range(0,k):
   print(end=" ")
   for j in range(0,3):
    print("*",end=" ")
   print()
first(7)
second(7)
steam(3)
© www.soinside.com 2019 - 2024. All rights reserved.