"斜交"在Java中使用星号的三角形

时间:2017-03-09 19:18:24

标签: java loops for-loop nested-loops

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

  • 我发布了自己的暂定资料。
  • 很好地解释了需要什么
  • 我对这个话题进行了广泛的研究,我刚刚发现其他类型的东西对于像我这样的初学者(几周前我开始使用Java)是完全不同的。
  • 老实说,我在这里看到的唯一问题是,人们贬低一个问题,因为他们没有读过这个帖子,或者因为他们在不知道发生了什么的情况下判断某人是懒惰的。

我正在学习Java。

我尝试下面的内容,输入一个数字,然后创建一个带星号的三角形。我已经弄清楚了,除了我的第二个循环没有给我我认为应该给出的东西(从给定范围到极限,它是递增而不是相反,它是' s让我疯了)。

我无法理解为什么它不像预期的那样工作。我只需要一双新鲜的眼睛(以及如何学会自己看这些东西的建议)。

此刻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("*");
            }
        }
    }
}

5 个答案:

答案 0 :(得分:2)

只需更改内循环:

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("*");
    }
}

结果:

Result

答案 1 :(得分:2)

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

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);
}

输出

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

答案 2 :(得分:0)

您可以尝试这样的事情:

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

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

Process finished with exit code 0

{{1}}的示例输出:

{{1}}

答案 3 :(得分:0)

将内循环更改为此。我对它进行了测试,效果很好:

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

答案 4 :(得分:-1)

你可以尝试

  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;
}