2013-10-22 47 views
0

所以基本上我的程序会在用户输入一个模块8和等于0的数字时打印这种类型的三角形。与4。这是一个基础做出来的小三角是怎样一个三角形的样子,当我输入数字16:我不知道如何给小三角形增加空格,使它成为一个更大的三角形

* * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * 
     * *  * *  * *  * * 
     *  *  *  * 
     * * * * * * * * * * * * 
     * * * * * * * * * 
      * *  * *  * * 
      *  *  * 
      * * * * * * * * 
      * * * * * * 
       * *  * * 
       *  * 
       * * * * 
       * * * 
        * * 
        * 

但我的竟然是这样的:

* * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * 
     * *  * *  * *  * * 
     *  *  *  * 
    * * * * * * * * * * * * 
    * * * * * * * * * 
     * *  * *  * * 
     *  *  * 
    * * * * * * * * 
    * * * * * * 
     * *  * *  
     *  *  
    * * * * 
    * * * 
     * * 
     * 

我不是所以确定如何添加空格。有人可以帮我吗?这里是我的代码:

... 
      System.out.println("Please enter the length:"); 
      int length = scan.nextInt();; 

      //length must be longer than 2 
      if (length <= 1){ 
       System.out.println("Sorry :(! Length must be 2 or higher"); 
      } 

      //if user enter a number that can divide by 8 and has no remainder, then 
      //loop to print out a beautiful triangle 
      else if(length%8==0) { 
       int numOfTriangle = length/4; 
       System.out.println("Here is your beautiful triangle :D \n"); 

       for (int rowOfTri = numOfTriangle; rowOfTri > 0; rowOfTri--){ 
        for(int i = 0; i < rowOfTri; i++){ 
         printBase(); 
        } 
        System.out.println(); 
        for(int i = 0; i < rowOfTri; i++){ 
         printThree(); 
        } 
        System.out.println(); 
        for(int i = 0; i < rowOfTri; i++){ 
         printTwo(); 
        } 
        System.out.println(); 
        for(int i = 0; i < rowOfTri; i++){ 
         printOne(); 
        } 
        System.out.println(); 
       } 
       System.out.println(); 
      } 

      //any other number will print a normal triangle 
      else{ 
       System.out.println("Here is your beautiful triangle :D \n"); 
       for (int i = 1; i <= length; i++){ 
        for (int j = 1; j <= length; j++){ 
         if (j < i){ 
          System.out.print(" "); 
         } else { 
          System.out.print("*"); 
          System.out.print(" "); 
         } 
        } 
        System.out.println(); 
       } 

      } 

      //asking users if they want to print again 
      System.out.println("\nDo you want to print another triangle? Type Yes or No."); 

      //scan for string answer 
      String againChoice = scan.next(); 

      //if answer start with Y or y then answer is Yes. 
      if(againChoice.startsWith("Y") || againChoice.startsWith("y")){ 
       printAgain = true; 
      } 

      //if answer start with N or n then answer is No. 
      else if(againChoice.startsWith("N") || againChoice.startsWith("n")){ 
       printAgain = false; 
       System.out.println("Bye!"); 
      } 

      // set input to none again 
     } while (printAgain == true); 

    } 
    //methods to print a triangle 
    public static void printBase(){ 
     System.out.print("* * * * "); 
    } 
    public static void printThree(){ 
     System.out.print(" * * * "); 
    } 
    public static void printTwo(){ 
     System.out.print(" * * "); 
    } 
    public static void printOne(){ 
     System.out.print(" * "); 
    } 
    public static void printSpace(){ 
     System.out.print(" "); 
    } 

} 
+0

也许只是一些额外的空格/制表符,以你的“*”当你开始绘制底部三角形? – Tafari

回答

0

我已经改变了您的打印这样的代码,它工作正常:

else if (length % 8 == 0) { 
      int layer = 0; 
      int numOfTriangle = length/4; 
      System.out.println("Here is your beautiful triangle :D \n"); 

      for (int rowOfTri = numOfTriangle; rowOfTri > 0; rowOfTri--) { 
       for (int i = 0; i < layer; i++) { 
        printSpace(); 
       } 
       for (int i = 0; i < rowOfTri; i++) { 
        printBase(layer); 
       } 
       System.out.println(); 
       for (int i = 0; i < layer; i++) { 
        printSpace(); 
       } 
       for (int i = 0; i < rowOfTri; i++) { 
        printThree(layer); 
       } 
       System.out.println(); 
       for (int i = 0; i < layer; i++) { 
        printSpace(); 
       } 
       for (int i = 0; i < rowOfTri; i++) { 
        printTwo(layer); 
       } 
       System.out.println(); 
       for (int i = 0; i < layer; i++) { 
        printSpace(); 
       } 
       for (int i = 0; i < rowOfTri; i++) { 
        printOne(layer); 
       } 
       System.out.println(); 
       layer++; 
      } 
      System.out.println();