0

您好,我想使用 java 循环创建一个三角形。但我需要的只是边界。我试过使用这种语法,但它总是显示错误。有什么建议吗?

int lines = 5;
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
  for (int j = i; j < lines; j++)
  {
    System.out.print(" ");
  }
  for (int k = 1; k <= c; k++)
  {
    if (k % 2 == 0)
    {
      System.out.print(" ");
    }
    else
    {
      if (k == 0 || k == lines - 1) {
        System.out.print("*");
      }
    }
  }

  System.out.print("\n");

  c -= 2;
}

你能帮帮我吗?谢谢

4

4 回答 4

3

你的代码有很多问题...

for (int k = 1; k <= c; k++) 
// and then:
if (k == 0 || k == lines - 1) {

k永远不会0

if (k % 2 == 0)
// and then 
else
{
  if (k == 0 || k == lines - 1) {

a 即使kwas 0, then 0%2 == 0, sok==0也永远不会发生

截至目前,您的应用仅打印空格。

如果你想画一个三角形,(可能)最简单的方法是

public static void main(String[] args) {
    int lines = 6;
    int cols = lines+1;

    for (int line =0; line < lines-1; line++) {
        for (int col = 0; col<cols; col++) {
            if (col == 0 || col == line) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
    for (int col=0; col<cols; col++) {
        System.out.print("*");
    }
    System.out.println();
}
于 2013-09-26T10:20:07.757 回答
0

三角.java

public class Triangle {
  private int lines;

  public Triangle(int lines) {
    this.lines = lines;
    display();
  }

  public void display() {
    int spaces = lines - 1;
    int last;
    int min = 1;
    int max;

    for (int i = 0; i < lines; i++) {
      last = 2 * i + 1;
      max = spaces + last;

      for (int j = min; j <= max; j++) {
        if (j == spaces + 1 || j == max || i == lines - 1)
          System.out.print('*');
        else
          System.out.print(' ');
      }

      System.out.println();
      spaces--;
    }
  }

  public static void main(String[] args) {
    new Triangle(3);
    new Triangle(4);
    new Triangle(5);
    new Triangle(6);
  }
}

输出

  *
 * *
*****
   *
  * *
 *   *
*******
    *
   * *
  *   *
 *     *
*********
     *
    * *
   *   *
  *     *
 *       *
***********
于 2013-09-26T10:54:11.253 回答
0

我使用了两种方法,并使用 for 循环重复了这两种方法。

public class OpenWord {
    int i; 
    String k = " ";
    
public void space(){
    System.out.println("*"+k+"*");
}

public void star(){
    System.out.println("*");
}

public void trainagle(){
    star();
    for ( i=1; i<=10;i++){
        space();
        k =k+" ";
        if(i==10){
            k=k.replace(" ", "*");
            space();
        }
    }
}

public static void main(String args[])   {
     OpenWord a = new OpenWord();
     a.trainagle();
}
}

输出:

*
* *
*  *
*   *
*    *
*     *
*      *
*       *
*        *
*         *
*          *
*           *
*            *
*             *
*              *
*               *
******************
于 2017-05-14T07:20:13.977 回答
0

前任:

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

代码:

int numberOfRows = 5;
    
for(int i=0; i<numberOfRows; i++)
{
    for(int a=0; a<=numberOfRows; a++)
    {
        int x = numberOfRows - i;
            
        if (a > x) {
           if (x==(a-1) || i==(numberOfRows-1)) {
               System.out.print("*");
           } else {
               System.out.print(" ");
           }
        } else {
           System.out.print(" ");
        }
    }
                    
    for(int b=0; b<=i; b++)
    {
        if(b==i || i==(numberOfRows-1)) {
            System.out.print("*");
        } else {
            System.out.print(" ");
        }
    }

    System.out.print("\n");
}
于 2021-04-26T03:59:50.140 回答