Java扫描仪与Star制作三角形(分为四个静态空隙



我必须做这样的事情。

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

但是我的老师必须以某种怪异的格式遵循。并用于循环语句。我需要键入的代码不在(//在此处使用...循环的回答)

import java.util.Scanner;
public class IsoTri3
{
    public static void main (String[] args) 
    {
        // declare viriable
        int height;
        Scanner sc = new Scanner(System.in);
        // Input height
        System.out.print("Enter the height of triangle: ");
        height = sc.nextInt();
        System.out.println(height);                 
        // Print triangle
        // Print Top
        topRow ( height );
        // Print Middle
        for (int j=2; j<= height-1; j++)
        {
            innerRow(j, height);
        }
        // Print Bottom
        lastRow( height );
    }
    //-------------------------------------------------------------------------//
    public static void topRow(int row)
    {
        //Answer Here using For...loop
    }
    public static void innerRow(int row,int h)
    {
        //Answer Here using For...loop
    }
    public static void lastRow(int h)
    {
        //Answer Here using For...loop
    }
}

您需要在此处找到模式。假设高度是n=5。所有行都有奇数(2*n - 1)星星。从第一行开始,它是这样的: 1星 3星 5星 7星> 9星。你看到模式了吗?在每一行,您必须打印 2星比上一行多。忘记格式。首先,在此处打印遵循此图案的星星。

接下来是格式化。我假设最后一行的空间在其前面。现在查看这里的模式。在第一行中,您必须打印5-1=4空间,第二行您有5-2=3空间,第三行,您的5-3=2空间,第四行,您有5-4=1空间,而第5(最后一个)行具有5-5=0空间。

编码这不是很棘手。

最新更新