Java 制作更小的圣诞树
所以我从java编程语言开始,我想打印一棵X高的圣诞树。到目前为止,它仍在工作,但如果用户输入4,它将打印4行+圣诞树树桩,即5行。但是,我希望它是4,包括树桩。到目前为止,我有:Java 制作更小的圣诞树,java,drawing,row,Java,Drawing,Row,所以我从java编程语言开始,我想打印一棵X高的圣诞树。到目前为止,它仍在工作,但如果用户输入4,它将打印4行+圣诞树树桩,即5行。但是,我希望它是4,包括树桩。到目前为止,我有: public class xmas { public static void main(String[] args) { Scanner scan = new Scanner(in); out.print("please enter a number: ");
public class xmas {
public static void main(String[] args)
{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
int temp = scan.nextInt();
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}
}
公共类xmas{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(英寸);
打印(“请输入一个数字:”);
int temp=scan.nextInt();
INTX=(温度-1)*2+1;
int y=x/2;
intz=1;
对于(int i=0;i尝试在输入之后使用temp--
,如下所示:
int temp = scan.nextInt();
temp--;
或减少循环条件:
for(int i=0; i<temp-1; i++)
如果您只是从输入中减去一个,您的圣诞树应该是正确的大小
公共类圣诞树{
公共静态void main(字符串[]args){
扫描仪=新扫描仪(英寸);
打印(“请输入一个数字:”);
int temp=scanner.nextInt()-1;//注意`-1`
INTX=(温度-1)*2+1;
int y=x/2;
intz=1;
对于(int i=0;i 对于(int j=0;j很好,你已经开始准备了;)这意味着什么?这是一个笑话-圣诞节离现在还有3个月;)你是如何得到圣诞树的想法的?不管怎样,你有没有考虑过在输入int temp=scan.nextInt();
之后使用temp--;
?
*
***
*****
*
public class ChristmasTree {
public static void main(String[] args) {
Scanner scanner = new Scanner(in);
out.print("Please enter a number: ");
int temp = scanner.nextInt() - 1; // note the `- 1`
int x = (temp - 1) * 2 + 1;
int y = x / 2;
int z = 1;
for(int i = 0; i < temp; i++) {
for(int j = 0; j <= y; j++) {
out.print(" ");
}
for(int j = 0; j < z; k++) {
out.print("*");
}
out.println();
y--;
z += 2;
}
for(int i =0; i<=x/2; i++) {
out.print(" ");
}
out.println("*");
}
}