Print pyramid of * in Java

问题: I'm wondering if you could help me out. I'm trying to write a nested for loop in java that displays a number pyramid triangle that looks like ___________*# __________*_*#...

问题:

I'm wondering if you could help me out. I'm trying to write a nested for loop in java that displays a number pyramid triangle that looks like

___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#

This is what I have so far:

class Triagle {
    public static void printTriagle(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = n - i; j > 1; j--) {
                System.out.print(" ");
            }

            for (int j = 0; j <= i; j++) {
                // printing stars
                System.out.print("* ");
            }

            System.out.println();
        }
    }

    public static void main(String[] args) {
        printTriagle(12);//I want to set the triangle to be height of 12
    }
 }

My result is not equal to the expected output:

___________*#
__________*_*#
_________*_*_*#
________*_*_*_*#
_______*_*_*_*_*#
______*_*_*_*_*_*#
_____*_*_*_*_*_*_*#
____*_*_*_*_*_*_*_*#
___*_*_*_*_*_*_*_*_*#
__*_*_*_*_*_*_*_*_*_*#
_*_*_*_*_*_*_*_*_*_*_*#
*_*_*_*_*_*_*_*_*_*_*_*#

回答1:

I have updated your code and added comments so that you can understand. Refer to the code below:

public static void printTriagle(int n) {
    for (int i = 0; i < n; i++) {

        for (int j = n - i; j > 1; j--) {
            System.out.print("_");
        }
        String s = "_";
        if (i + 1 >= n) // check if it is the last line
            s = "*"; // change to print * instead of _

        for (int j = 0; j <= i; j++) {
            // printing stars
            if (j == i)
                System.out.print("*#"); // check if the last print of the line
            else if (j == 0)
                System.out.print("*" + s); // check if the first print of the line
            else
                System.out.print(s + s);
        }

        System.out.println();
    }
}

Result:

___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#

回答2:

Try this

public static void printTriagle(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = n - i; j > 1; j--) {
            System.out.print(" ");
        }

        for (int j = 0; j <= i; j++) {
            // printing stars
            if(i == (n-1)){
                System.out.print("**");
            }
            else{
                System.out.print((j == 0 || j == i) ? "* " : " ");
            }

        }
        System.out.println();
    }
}

回答3:

Your issue is here:

for (int j=0; j<=i; j++){
     // printing stars
     System.out.print("* ");
}

Here, it prints a star for each number between 0 and i, but it only should print a star if it is exactly 0 or i.

Try something like this:

for (int j=0; j<=i; j++){
     if ( i == n ) {
       System.out.print("* ");
     } else {
       System.out.print(j == 0 || j == i ? "* " : "  ");
     }
}

EDIT: You may still have to adapt your code to get the bottom line printed correctly, in case that line has to be all stars


回答4:

This is what you need to do:

public static void printTriagle(int n) {
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < 2*n; j++) {
            if(i == n-1) {
                System.out.print((j != 2*n-1) ? "*" : "#");
            }
            else {
                if(i+j == n-1) {
                    if(i == 0) {
                        System.out.print("*#");
                        break;
                    }
                    else {
                        System.out.print("*");
                    }
                }
                else if(j-i == n-1) {
                    System.out.print("*#"); break;
                }
                else {
                    System.out.print("_");
                }
            }
        }
        System.out.println();
    }
  • 发表于 2019-03-05 18:19
  • 阅读 ( 236 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除