三角形由星号组成

时间:2014-02-10 23:48:20

标签: java input geometry

我已经看到了一些帖子,但我似乎不明白问题是什么。我对此非常陌生,我无法弄清楚这一点。我必须用星号做一个三角形作家庭作业。行数将由用户输入和显示的整数确定,如下所示

*
**
***
****

我不确定该怎么做。但这是我到目前为止(非常确定我犯了愚蠢的错误)

import java.util.Scanner;


public class triangle {
    public static void main(String args[]) {
        /*declarations*/
        Scanner input= new Scanner(System.in);
        int how_many;/*number of rows*/
        int i;
        int j;
        /*prompt for input*/
        System.out.printIn("Choissiez un nombre entier postif");
        how_many=input.nextInt();
    }
        for(i=1;i<=1;i++){
            for(j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.printIn();
        }

}

3 个答案:

答案 0 :(得分:2)

这个怎么样:

import java.util.Scanner;

public class triangle {
    public static void main( final String args[] ) {
/*declarations*/
        Scanner input = new Scanner( System.in );
        int how_many;/*number of rows*/
        int i;
        int j;
/*promt for input*/
        System.out.println( "Choissiez un nombre entier postif" );
        how_many = input.nextInt();
        for( i = 1; i <= how_many; i++ ) {
            for( j = 1; j <= i; j++ ) {
                System.out.print( "*" );
            }
            System.out.println();
        }
    }
}

使用jdk 7。

主要的算法问题是您需要设置i <= how_manyj <= i

答案 1 :(得分:0)

编写代码时。将开始和结束括号{ }保持在同一行。

您可以更轻松地阅读代码。

此外,您还可以看到代码的位置。

这是您尽可能格式化的代码。

public class triangle
{

    public static void main(String args[])
    {
        /*declarations*/
        Scanner input= new Scanner(System.in);

        /*
         * No need to create this variable here.
         * Just create a variable when you need it.
         *
        int how_many;/*number of rows/
         */

        /*
         * The i and j variables can be declared right within the 
         *  for loop.
         * 
        int i;
        int j;
         */

        /*
         * It's println, not printIn
         */
        /*prompt for input*/
        System.out.println("Choissiez un nombre entier postif");

        /*
         * You can just create the variable right here, int how_many
         */
        int how_many=input.nextInt();

        /*
         * This for loop is doing nothing. It runs once, thats it.
         * 
         * You want it to loop as many times as the user entered
         *  
        for(int i = 1 ; i <= 1 ; i++)
         */
        for(int i = 1 ; i <= how_many ; i++)
        {
            /*
             * In this nested loop you need to print out the number of
             *  asterisks depending on the iteration of the parent loop
             * 
            for(int j=1; j<=i; j++)
             */
            for(int j = 1 ; j <= i ; j++)
            {
                System.out.print("*");
            }

            System.out.println();
        }
    }

    /*
     * This entire for loop is not within any method.
     * 
     * You need to bring it a few lines
     * 
    for(i=1;i<=1;i++)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print("*");
        }

        System.out.printIn();
    }
     */
}

或者

public class triangle
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Choissiez un nombre entier postif");

        int how_many = input.nextInt();

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

或者

public static void main(String args[])
{
    System.out.println("Choissiez un nombre entier postif");

    Scanner input = new Scanner(System.in);
    int how_many = input.nextInt();

    for(int i = 1; i <= how_many; i++)
        System.out.println(String.format("%0" + i + "d", 0).replace("0", "*"));

    input.close();
}

我能得到最好的

public class triangle
{
    public static void main(String args[])
    {
        int n = new Scanner(System.in).nextInt();
        while(n != 0)
            System.out.println(String.format("%0" + n-- + "d", 0).replace("0", "*"));
    }
}

答案 2 :(得分:0)

以下是我几年前的类似项目的代码。你必须添加一个主要内容,但这不应该太难。这段代码很有效,而且非常明显。如果您有任何问题,请告诉我们。)

public class TriangleTwo
{
    private int size; //size
    private String letter; //symbol or letter

public TriangleTwo()
{

}

public TriangleTwo(int count, String let)
{
    setTriangle(count, let);
}

public void setTriangle(int count, String let)
{
    size = count;
    letter = let;
}

public String getLetter()
{
    return "*"; //you can change the symbol that is printed
}

public String toString()
{
    String output="";
    for(int i = size; i >= 1; i--) // transverse through col
    {
        for(int x = i; x >= 1; x--) // transverse through row
        {    
            output += letter; // will print the letter that you chose
        }
        output += "\n"; // skips a line
    }
    return output + "\n"; // prints triangle
}
}