实操名称:将上面打印实心和空心正方形联系中的相关代码,封装成方法,便于重用
环境: jdk 1.8
作者:李远远
1.详细代码如下:
/**
* 将打印实心和空心正方形练习中相关的代码,封装成方法,便于重用
* @author 李远远
*
*/
public class Test09 {
public static void main(String[] args) {
printSoild();//调用打印实心正方形的函数
printHollow();//调用打印空心正方形的函数
printSoild();
}
private static void printSoild(){
System.out.println("打印实心正方形:");
//打印实心正方形
for(int i=1;i<=10;i++) {//控制实心正方形的行
for(int j=1;j<=10;j++) {//控制实心正方形的列
System.out.print("*"+" ");
}
System.out.println();
}
}
private static void printHollow(){
System.out.println("打印空心正方形:");
//打印空心正方形
for(int k=1;k<=10;k++) {
if(k>1&&k<10) {
System.out.println("* *");
}else {
System.out.println("* * * * * * * * * *");
}
}
}
}
2.运行结果动图如下:
3.Bug及心得体会:
函数的封装还不太懂,敲的时候有点费力,在定义的函数的时候,函数的类型要搞清楚,名称的第二个字母要大写。遇到不懂的,还不熟练的东西,要多敲几遍,这样才会一步一步的到很好的运用它,掌握它。