Java 中的金字塔模式,后跟星形后跟下划线

我想打印以下图案:


*

*_*

*_*_*

*_*_*_*

*_*_*_*_*

但这里的问题是我无法消除最后一个符号,这是我得到的输出:_


*

*_*_

*_*_*_

*_*_*_*_

*_*_*_*_*_

int row =5;

String star="*";

String undrscr="_";

String s;

for(int i=1;i<=row;i++)

{//System.out.print(undrscr);

    for(int j=1;j<=i;j++)

    {           

        if(i==1)

        {

        System.out.print(star);

        }

        if(i>1)

        {

            System.out.print(star+""+undrscr);

        if(j==i)

        {

            System.out.print("");

        }

        }

    }


    System.out.println();

}


海绵宝宝撒
浏览 86回答 4
4回答

慕的地10843

将你的内在 for 循环更改为:for (int i = 1; i <= row; i++) {&nbsp; &nbsp; for (int j = 1; j <= i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(star);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (i > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(star);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i - 1 >= j) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // <-- change made in this if-block only&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(undrscr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j == i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}在打印下划线之前,请检查是否因为根据您的要求下划线数始终比星数少1。i - 1 >= j

慕婉清6462132

试试这个..&nbsp; &nbsp; int row = 5;&nbsp; &nbsp; String star = "*";&nbsp; &nbsp; String undrscr = "_";&nbsp; &nbsp; String s;&nbsp; &nbsp; for (int i = 1; i <= row; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j <= i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(star);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i != j)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(undrscr);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }

回首忆惘然

试试这个://i was the rowNum and the number of stars of that rowfor (int rowNum = 0; rowNum<=rows; rowNum++) {&nbsp; &nbsp; int numStars = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (numStars<rowNum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(star+undrscr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numStars++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(star);&nbsp; &nbsp; }输出:**_**_*_**_*_*_**_*_*_*_**_*_*_*_*_*编辑:最好使用这种方法,你会得到5行而不是6行。for (int rowNum = 1; rowNum<=rows; rowNum++) {&nbsp; &nbsp; int numStars = 1;&nbsp; &nbsp; while (numStars<rowNum) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(star+undrscr);&nbsp; &nbsp; &nbsp; &nbsp; numStars++;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(star);}

慕村9548890

在此,星星的行数呈递增顺序(即第一行有 1 颗星,第二行为 2 颗星,依此类推)。逐步描述性逻辑,以打印带有下划线图案的直角三角形星形。1)输入用户要打印的行数。我们传递 5 行。2)要循环访问行,请使用循环结构 for(i=0; i=5<; i++) 运行一个从 0 到 5 的外部循环。3)要循环访问列,请使用循环结构 for(j=0; j=0;< j=i; j++) 运行一个从 0 到 i 的内部循环。内部循环内部打印星形。4)在列循环中,我们检查i == j是否然后只打印星形,否则打印星形和下划线。5)打印一行的所有列后,移动到下一行,即打印新行。&nbsp;int i, j;&nbsp; &nbsp; &nbsp; for(i=0; i<5; i++)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(j=0; j<=i; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(i==j)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print("*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("_");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java