0

我必须编写一个代码来打印一个三角形,最后一行的长度是输入数字。空白处也必须用点填充。

它必须看起来像这样。

此外,代码必须一直运行到输入为奇数为止。

可悲的是,我的代码没有打印出点或其他星星,而只是最后一行。

有人可以看看它并给我一些提示吗?

任何帮助表示赞赏。非常感谢您提前。

这是代码:



import java.util.Scanner;

public class Triangle_new {

    //Main-Method with Input-Check

public static void main(String[] args) {

    System.out.println("Type in a number to get the triangle:");


        Scanner sc = new Scanner(System.in);

        int length = sc.nextInt();



        if(length < 0 || length%2 == 0) {

                System.out.println("Please type in a uneven number or zero to quit. ");
                Triangle_Test.check_input(length);

        }else{
            display_triangle(length);
             }  

    }

    //DisplayTriangle-Method


    public static void display_triangle(int length) {

    int lines = (length+1)/2;

        //Height of the Triangle
    get_lines(length);

        //Dotfiller Left Side
    if(lines > 0){
    get_dotsleft(lines, length);
    }
        //Stars-Output
    if(lines > 0) {
    get_stars(lines);
    }
        //Dotfiller Right Side
    if(lines > 0) {
    get_dotsright(lines, length);
    }

    }

    //Constructors for Triangle

    public static void get_lines(int length) {

        for(int lines = 1; lines <= (length + 1) / 2; lines++){
        System.out.println();
        }
    }

    public static void get_dotsleft(int lines, int length){
        for(int leftfiller = 1; leftfiller <= ((length + 1) / 2) - lines; leftfiller++) {
        System.out.print(".");

        }
    }

    public static void get_stars(int lines) {
        for(int stars = 1; stars <= (2 * lines) - 1; stars++) {
        System.out.print("*");

        }
    }



    public static void get_dotsright(int lines, int length){
        for(int rightfiller = 1; rightfiller <= ((length+1) / 2) - lines; rightfiller++) {
        System.out.print(".");
        }
    }


}


    //Triangle_Test Class with Input-Check Method

class Triangle_Test extends Triangle_new {

    public static void check_input(int length) {


    //Check Input until Input is uneven number  

    Scanner sc = new Scanner(System.in);

    do {

    length = sc.nextInt();

    if(length%2 == 0 && length != 0) {
        System.out.println("Invalid input. Try again.");
                      }


    if(length%2 != 0 && length > 0) {

                    }

    //Triangle Output               

    if(length%2 != 0){

    Triangle_new.display_triangle(length);  

    }               

    if(length == 0) { 
    System.out.println(" -> Zero detected. Program will be shutdown.");
    }



    }while(length%2 != 0 || length != 0);

    }

}



4

3 回答 3

0

以下是您的代码中的问题:

  1. get_lines 只是在 for 循环中打印一些新行,而在每个星/点组合之后我们只需要一个新行。换句话说,新行的数量不会改变。它总是每行一个。

  2. get_dotsLeft 和 get_dotsRight 实际上是相同的。它们应该只组合在一个功能中。

  3. 循环打印点和星的限制不正确。我修好了它们。

  4. 按照惯例,在命名函数时最好遵循驼峰式命名,即 get_lines 应该是 getLines。

  5. 你所有的 get_ 函数都应该是私有的。它们应该只能在您的班级中访问,而不应该暴露给其他班级。(这不会引发错误,但将这些功能公开并不是一个好的设计)。

    public static void display_triangle(int length) {
    int lines = (length+1)/2;
    for (int i =1 ; i<= lines ;i++) {
        //Height of the Triangle
        get_lines(length);
        get_dotsleft(lines-i);
        get_stars(i);
        get_dotsright(lines-i);    
      }
    }
    
    //Constructors for Triangle
    private static void get_lines(int length) {                     
      System.out.println(""); 
    }
    
    private static void get_dotsleft(int lines){
        for(int leftfiller = 1; leftfiller <= lines; leftfiller++) {
            System.out.print(".");
        }
    }
    
    private static void get_stars(int lines) {
        for(int stars = 1; stars <= (2 * lines) - 1; stars++) {
            System.out.print("*");
        }
    } 
    private static void get_dotsright(int lines){
        for(int rightfiller = 1; rightfiller <= lines; rightfiller++) {
            System.out.print(".");
        }
    }
    
于 2019-11-25T14:20:09.290 回答
0

您的get_lines()方法听起来应该返回三角形将填充多少行(也基于您的评论),但它的作用是打印空行。尝试不使用它。

于 2019-11-25T09:32:27.060 回答
0

按如下方式更改您的 display_tringe 函数:

public static void displayTriangle(int length) {
        String stars = "*";
        for (int i =0 ; i< ((length+1) /2); i++){
            String dots = "";
            for (int j = 0; j < (length - i * 2) / 2; j++) {
                    dots += ".";
                }
            System.out.println(dots+stars+dots);
            stars +="**";
        }
}

让我解释一下为什么您的解决方案不起作用:

  1. 您的 get_lines 循环仅打印空行。
  2. 您应该在一个大循环中显示所有内容(表示行数),并在此循环中计算要显示的星号和点数。
  3. 请注意:每行中的点数+星数始终等于用户输入的总数。
  4. 左边的点数总是等于星星右边的点数,每边=(用户输入的总数-星星数)/2。
  5. 每行将星数增加 2。

这就是我思考问题的方式,因此得到了我的解决方案。

于 2019-11-25T09:32:37.380 回答