有人可以告诉我该怎么做

有人可以告诉我该怎么做

本文介绍了有人可以告诉我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要它是这样的:





I need it to be like this:


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





但是我不知道如何减少2行的每一行,所以它与第一行的长度相同



我尝试了什么:



所以基本上这是我的代码:





but I cannot find out how to decrement every line by 2 stars so it comes the same length as first line

What I have tried:

So basically this is my code:

for (int i = 0; i<=5; i++){

          if (i == 1 || i == 3){
              System.out.print(" ");
              System.out.print(" ");

            }
            if (i == 2){
           System.out.print(" ");
           System.out.print(" ");
           System.out.print(" ");
           System.out.print(" ");

        }

         for (int j = 0; j <=12; j++){
               System.out.print("*");
        }
             System.out.println();
    }


}


}

推荐答案

final int ROWS = 2;
final int COLS = 12;

for ( int row = - ROWS; row <= ROWS; ++row)
{
  for (int col = 1; col <= COLS; ++col)
  {
    int limit = row >= 0 ? ROWS - row : ROWS + row;
    limit *= 2;
    char c = col <= limit ? ' ' : '*';
    System.out.print(c);
  }
  System.out.println();
}


这篇关于有人可以告诉我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 04:53