用for语句怎么编写出这个图形

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/04 04:37:02
用for语句怎么编写出这个图形

用for语句怎么编写出这个图形
用for语句怎么编写出这个图形

用for语句怎么编写出这个图形

代码给你贴在下面吧,你自己看下

public class K
{

    public static void main(String[] args)
    {
        int len = 2; // 菱长
        String outstr = " "; // 输出
        int i, j, x, y;
        for (i = 0; i <= len * 2; i++)
        {
            for (j = 0; j <= len * 2; j++)
            {
                x = len - i;
                x = x < 0 ? -1 * x : x;
                y = len + (len - x);
                outstr += (j == x || j == y ? "*" : " ");
            }
            outstr += "\n ";
        }
        System.out.println(outstr);
    }
}