声明一个method方法,打印m*n的星型矩阵,再计算该矩阵面积
将其作为方法返回值,在main方法中调用该方法,接受返回的面积值并打印
1、方法中形参定义m、n,调用时m、n作为矩阵长、宽
public class Exer11 {
public static void main(String[] args) {
Exer11 test = new Exer11();
int area = test.method(10,5);
System.out.println("面积为:" + area);
}
public int method(int m,int n) {//定义形参,即矩阵长、宽值
for(int i = 0;i < m;i++) {
for(int j = 0;j < n;j++) {
System.out.print("* ");
}
System.out.println();
}
return m * n;
}
}
2、有实际数值的情况,不用再定义形参,直接简单输出即可
public class Exer11 {
public static void main(String[] args) {
Exer11 test1 = new Exer11();
//test1.method(); 错误、此时只打印星形矩阵
int area = test1.method();
System.out.println("面积为" + area);
//也可直接调用输出方法
//System.out.println(test1.method());
}
public int method() {
for(int i = 0;i < 8;i++) {
for(int j = 0;j < 4;j++) {
System.out.print("* ");
}
System.out.println();
}
return 8 * 4;
}
}