需要帮助在java工作中为等边三角形制作代码

时间:2015-09-08 18:27:27

标签: java loops for-loop

由于某种原因,只有奇数行正确间隔。有人可以解释一下吗?这是我的代码。

import java.util.Scanner;
public class Triangle {


    public static void main(String args[]) {
        int i, j, k1;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the length of the side of your equilateral triangle: ");
        int side = in.nextInt();
        System.out.println("Enter the character you want your triangle filled with: ");
        char fill = in.next().charAt(0);
        while (side<=0 || side>50){
            System.out.print("Please enter a length between 1 and 50: ");
            side = in.nextInt();
            break;
        }
        for(i=1; i<=side;i++){
            for (k1=0; k1 < (side-i/2);k1++){
                System.out.print("\t");
            }
            for(j=1;j<=i;j++){
                System.out.print(fill + "\t");
            }
            System.out.println('\n');
        }
        in.close();
    }
}

这是我的结果:     输入等边三角形边长:     五     输入您希望三角形填充的字符:     *

                *   

            *   *   

            *   *   *   

        *   *   *   *   

        *   *   *   *   *

我想要这个:

    # 
   # # 
  # # # 
 # # # # 
# # # # # 

3 个答案:

答案 0 :(得分:3)

只需添加此项并将/ t更改为4个空格:

if(i%2 ==0 && j ==1){
     System.out.print("  "); // two spaces
}

完整代码

    for(i=1; i<=side;i++){

        for (k1=0; k1 < (side-i/2);k1++){
            System.out.print("    ");
        }
        for(j=1;j<=i;j++){

            if(i%2 ==0 && j ==1){
                System.out.print("  ");
            }
            System.out.print(fill + "    ");
        }
        System.out.println('\n');
    }

原因是因为每个偶数行都需要以一点点偏移量开始,因为它必须位于填充符的中间并且我得到了这个

                                *    

                              *    *    

                            *    *    *    

                          *    *    *    *    

                        *    *    *    *    *    

                      *    *    *    *    *    *    

                    *    *    *    *    *    *    *    

                  *    *    *    *    *    *    *    *    

                *    *    *    *    *    *    *    *    *    

答案 1 :(得分:1)

您尚未说明所需的结果,但删除/2可能会解决问题。

也许用空格替换\t

'\n'移除println,除非您想在行之间留空行。

答案 2 :(得分:1)

这个怎么样:

try (final Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name())) {
  System.out.print("Enter the length of the side of your equilateral triangle: ");

  final int side = scanner.nextInt();

  System.out.print("Enter the character you want your triangle to be filled with: ");

  final char character = scanner.next().charAt(0);

  for (int i = 1; i <= side; i++) {
    final StringBuilder builder = new StringBuilder(side);

    for (int k = 0; k < (side - i); k++) {
      builder.append(' ');
    }
    System.out.printf(builder.toString());
    for (int j = 0; j < i; j++) {
      System.out.printf("%s ", character);
    }
    System.out.println();
  }
}

请注意Scanner的try-with-resources - 仅在Java 7(及以后版本)中提供。