我有这个java代码,基本上只是打印X高度的圣诞树。然而,程序要求的数字,然后打印树,然后就结束了。我会喜欢它循环,直到我输入0,这将结束该程序,并且我还希望只在输入的数字是1-40(不超过40)时才打印它。我开始在java世界中,但我不知道怎么办that.Heres我的代码现在:循环程序,直到0(退出)(java)
public class xtree {
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-1; 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("*");
}
}
谢谢你,我是一个初学者到Java,所以请手下留情;)
感谢您的帮助,这个工作,但是我去了输入0,这仍然返回错误信息请输入1和40之间的数字!退出前;我怎么能不输入错误信息就退出?感谢 – 2014-09-07 18:29:44
我已经编辑了答案,只是改变 ...}否则{... 到 }否则,如果(温度!= 0){ 这应该现在的工作;) – Philipp 2014-09-08 19:35:44