"Skewed" Java 中带星号的三角形

"Skewed" Triangle with asterisks in Java

我真的不明白为什么所有的反对票:

我在学习Java。

我正在尝试下面的操作,您输入一个数字,它会创建一个带星号的三角形。我已经很清楚了,除了我的第二个循环没有给我我认为它应该给的东西(从给定的范围到一个极限,它是递增的而不是相反的,这让我发疯)。

我想不通为什么它没有按预期工作。我只需要一双新的眼睛来看待它(以及如何学会自己看那些东西的建议)。

此时3的输出为:

*
**
***
*
**

而不是

*
**
***
**
*

下面是我的代码,以及我试图做的事情的解释:

/*

    Write a program that asks the user to enter the size of a triangle (an integer
    from 1 to 50). Display the triangle by writing lines of asterisks. The first
    line will have one asterisk, the next two, and so on, with each line having one
    more asterisk than the previous line, up to the number entered by the user.
    On the next line write one fewr asterisk and continue by decreasing the number
    of asterisk by 1 for each successive line until only one asterisk is displayed.
    Hint: use nested for loops; the outside loop controls the number of lines to
    write, and the inside loop controls the number of asterisks to display on a line)
    For example, if the user enters 3, the output would be:
    *
    **
    ***
    **
    *

*/

import java.util.Scanner;

public class Triangles
{
    public static void main (String[] args)
    {
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter the number of your triangle (1 to 50):");
        int userInput = kb.nextInt();
        int minus = userInput -1;
        int lineNumber = userInput + minus;
        int half = (lineNumber / 2) + 1;

        for(int i = 1; i <= half; i++)
        {
            System.out.println("");
            for (int j = 1; j <= i; j++)
            {
                System.out.print("*");
            }
        }
        for (int i = minus; i >= 1; i--)
        {
            System.out.println("");
            for (int j = minus; j >= i; j--)
            {
                System.out.print("*");
            }
        }
    }
}

你可以试试

  for (int i = minus; i >= 1; i--)
        {
            System.out.println("");
            for (int j = minus; j >= 0 /* it was i */; j--)
            {
                System.out.print("*");
            }
        }

int add = 1;
int asterics = 1;
while(asterics >0){
   if(asterics == userInput)
   {
      add = -1; // switch to decrement
   }
   for(int i=0;i<asterics;i++){
      System.out.print("*");
   }
   asterics += add;
}

只需更改内循环:

for (int i = minus; i >= 1; i--)
{
    System.out.println("");
    for (int j = 1; j <= i; j++)
    {
        System.out.print("*");
    }
}

虽然外循环是递减的,但内循环应该是递增的

整个代码将是:

Scanner kb = new Scanner(System.in);
System.out.println("Enter the number of your triangle (1 to 50):");
int userInput = kb.nextInt();//3
int minus = userInput -1;//2
int lineNumber = userInput + minus;//5
int half = (lineNumber / 2) + 1;//3

for(int i = 1; i <= half; i++){
    System.out.println("");
    for (int j = 1; j <= i; j++){
        System.out.print("*");
    }
}
for (int i = minus; i >= 1; i--){
    System.out.println("");
    for (int j = 1; j <= i; j++){
        System.out.print("*");
    }
}

结果:

您可以尝试这样的操作:

Scanner kb = new Scanner(System.in);
System.out.print("Enter the number of your triangle (1 to 50): ");
int input = kb.nextInt();
for (int i = 0; i < input; i++) {
  System.out.println();
  for (int j = 0; j <= i; j++) {
    System.out.print("*");
  }
}
for (int i = input - 1; i > 0; i--) {
  System.out.println();
  for (int j = 0; j < i; j++) {
    System.out.print("*");
  }
}

5 的示例输出:

Enter the number of your triangle (1 to 50): 5

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

Process finished with exit code 0

将内循环改成这样。我测试了它,效果很好:

        //for (int j = minus; j >= i; j--) // <--old line
        for (int j = 0; j<i; j++) //<--new line
        {
            System.out.print("*");
        }

这是一个很好的简短方法:

private static void test(int n) {
    char[] buf = new char[n];
    Arrays.fill(buf, '*');
    for (int row = 1 - n; row < n; row++)
        System.out.println(new String(buf, 0, n - Math.abs(row)));
}

测试

public static void main(String[] args) {
    test(5);
}

输出

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