第六届南桥杯JavaA组 第五题 打印菱形

给出菱形的边长,在控制台上打印出一个菱形来。
为了便于比对空格,我们把空格用句点代替。
当边长为8时,菱形为:

.......*
......*.*
.....*...*
....*.....*
...*.......*
..*.........*
.*...........*
*.............*
.*...........*
..*.........*
...*.......*
....*.....*
.....*...*
......*.*
.......*

下面的程序实现了这个功能,但想法有点奇怪。
请仔细分析代码,并填写划线部分缺失的代码。

public class A
{
    public static void f(int n)
    {
        String s = "*";
        for(int i=0; i<2*n-3; i++) s += ".";
        s += "*";

        String s1 = s + "\n";
        String s2 = "";

        for(int i=0; i<n-1; i++){
            //System.out.println("=>"+s);
            s = "." + _____________________________________ + "*";  //填空
            s1 = s + "\n" + s1;
            s2 += s + "\n";
        }
        System.out.println(s1+s2);      
    }
    // main函数是我为了方便测试加上去的
    public static void main(String[] args)
    {
        f(8);
    }
}

注意,只能填写缺少的部分,不要重复抄写已有代码。不要填写任何多余的文字。

把原题中的注释打开一看就明白要填什么了,也挺水的,不多说
答案是s.substring(0, s.length() - 3)

猜你喜欢

转载自blog.csdn.net/wugufeng0220/article/details/44996655