我正在尝试打印一个错位的金字塔。

huangapple go评论87阅读模式
英文:

I´m trying to print a misaligned pyramid

问题

Sure, here's the translated code as you requested:

我在尝试打印一个带有不对齐间距的金字塔就像这样

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

我目前正在使用以下代码它给我一个完全对齐的金字塔但我期望的结果是上面我指定的间距

        
        //Chueca
        for (int i = 1, j = 0; i <= numLineas; ++i, j = 0) {
         
            for (int espacio = 1; espacio <= numLineas - i; ++espacio) {
                System.out.print(" ");
            }
            
            while (j != i) {
                System.out.print("*");
                ++j;
            }            
            System.out.println();
        
        }`
有没有办法修改这段代码以实现这一点
英文:

I´m having a little bit of trouble printing a pyramid with misaligned spacing, kinda like this:

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

I´m currently using this code which gives me a perfectly aligned pyramid, but my desired result is the spacing I specified above

    //Chueca
        for (int i = 1, j = 0; i &lt;= numLineas; ++i,j=0){
     
       for(int espacio = 1; espacio &lt;= numLineas-i; ++espacio){
            System.out.print(&quot; &quot;);
        }
        
        while(j != i){
            System.out.print(&quot;*&quot;);
            ++j;
        }            
        System.out.println();
    
    }`

IS there any way to modify this code to achieve this?

答案1

得分: 0

更改第二个“for”循环的条件,只打印一半的空格:

```lang-java
for (int espacio = 1; espacio <= (numLineas - i) / 2; ++espacio) {

输出

  *
  **
  ***
  ****
  *****
  ******
英文:

Change the condition of the second for loop to only print half the spaces:

for (int espacio = 1; espacio &lt;= (numLineas - i) / 2; ++espacio) {

Output

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

huangapple
  • 本文由 发表于 2020年8月24日 09:10:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63553508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定