2014-09-06 72 views
0

我有这个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

使用while循环:

Scanner scan = new Scanner(System.in); 
    System.out.print("please enter a number: "); 

    int temp = scan.nextInt(); 
    while (temp>0) { 
     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++) 
      { 
       System.out.print(" "); 
      } 
      for(int k = 0; k<z; k++) 
      { 
       System.out.print("*"); 
      } 
      System.out.println(); 
      y--; 
      z+=2; 
     } 
     for(int i =0; i<=x/2; i++) 
     { 
      System.out.print(" "); 
     } 
     System.out.println("*"); 
     temp = scan.nextInt(); 
    } 
3

嗯,我会的方式进行分离成两个:

  • 一个printChristmasTree方法,它接受的高度作为参数
  • main方法,它只是记录用户输入并调用printChristmasTree交易或退出

大多数当前的main方法将进入printChristmasTreemain将b一个循环。喜欢的东西:

public static void main(String[] args) { 
    Scanner scan = new Scanner(in); 
    while (true) { 
     System.out.print("Please enter a number (0-40): "); 
     int height = scan.nextInt(); 
     if (height == 0) { 
      // Exit the program 
      return; 
     } else if (height >= 1 && height <= 40) { 
      printChristmasTree(height); 
     } else { 
      System.out.println("Invalid input."); 
     } 
    } 
} 

有可以使用的,而不是从while (true)环回其他的方法,但是这看起来最简单的给我。

在我看来,将“输入输入”与“打印圣诞树”方面的分离导致了更多的可读代码,而不是将它们合并在一起,并且在诸如编写不同程序打印全部有效的圣诞树。

0

下面的代码做这件事:

public class xtree { 

    public static void main(String[] args) 
    { 
     int temp; 
     do{ 
      Scanner scan = new Scanner(in); 
      out.print("please enter a number: "); 
      temp = scan.nextInt(); 

      if(temp >= 1 && temp <= 40){ //don't display a tree higher than 40 
       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("*"); 
      }else if(temp != 0){ 
       out.print("Please enter a number between 1 and 40!"); 
      } 
     }while(temp != 0); 
    } 
} 
+0

感谢您的帮助,这个工作,但是我去了输入0,这仍然返回错误信息请输入1和40之间的数字!退出前;我怎么能不输入错误信息就退出?感谢 – 2014-09-07 18:29:44

+0

我已经编辑了答案,只是改变 ...}否则{... 到 }否则,如果(温度!= 0){ 这应该现在的工作;) – Philipp 2014-09-08 19:35:44