使用用户输入和循环打印圣诞树

时间:2023-01-13 12:17:11

I need to create a program that lets the user put in what they want the height of the Christmas tree to be. Then it needs to print the Christmas with a trunk attached to it.

我需要创建一个程序,让用户输入他们想要的圣诞树的高度。然后它需要打印圣诞节与附加的树干。

I got the Christmas tree part down, but I'm having issues with how I should approach centering the trunk underneath the tree with respect to the tree's actual output.

我得到了圣诞树的一部分,但我遇到的问题是我应该如何在树的实际输出方面使树干下方居中。

If someone could point me in the right direction, it would be greatly appreciated.

如果有人能指出我正确的方向,我将不胜感激。

Thank you in advance.

先感谢您。

Here is my code so far:

这是我到目前为止的代码:

package Homework8;

import java.util.Scanner;

public class Merry_Christmas2 {
	public static void main(String[]args){
		
		System.out.println("Welcome to Christmas Tree!\n");
		
		Scanner input = new Scanner(System.in);
		System.out.println("How tall do you want your tree to be?");
		int height = input.nextInt();
		
		
		for(int i = 0; i < height;i++){
			for(int j = 0; j< height - i; j++){
				System.out.print(" ");
			}
			for(int k =0; k <= i; k++){
				System.out.print("* ");
			}
			System.out.println();
		}
			for(int i = 0; i <= height;i++){
				for(int j = 0; j >= height;j++){
					System.out.print(" ");
				}
				for(int k = 1; k < 2; k++){
					System.out.print("*");
				}
				System.out.println();
			}
		input.close();	
		}
}

1 个解决方案

#1


0  

You have an error in your for j loop:

您的for j循环中有错误:

for(int j = 0; j >= height;j++){

Change >= to < and check if it works.

将>更改为 <并检查是否有效。< p>

#1


0  

You have an error in your for j loop:

您的for j循环中有错误:

for(int j = 0; j >= height;j++){

Change >= to < and check if it works.

将>更改为 <并检查是否有效。< p>