shanggui191
1:三大步
主类
属性
方法
例子:
public class juxingTest {
//主类
public static void main(String[] args) {
solute so=new solute();
so.method();
}
}
//写一个方法类 在主类中实例化solute后,可以调用method方法。
class solute {
public void method() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 8; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
第二种:跟上面这个类似
/*因为直接写在一个类里面了,所以
直接实例化这个类也可以,也可以成功运行。
*/
public class juxingTest {
public static void main(String[] args) {
juxingTest test=new juxingTest();
test.method();
}
public void method() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 8; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
题目实战
package com.atguigu.java.day08.exer;
/*
3.1 编写程序,声明一个method方法,在方法中打印10*8的*型矩阵,在main方法中调用该方法。
3.2修改上一个程序,在method方法中,除打印一个10*8的*型矩形外,再计算该矩形的面积,
并将其作为方法返回值.在main方法中调用该方法,接受返回的面积值并打印。
3.3膝盖上一个程序,在method方法中提供m和n两个参数,方法中打印一个m*n的*型矩形,
并计算该矩形的面积,将其作为方法返回值,在main方法中调用该方法,接收返回的面积值并打印。
*/
public class juxingTest {
public static void main(String[] args) {
juxingTest test=new juxingTest();
//3.1测试
// test.method();
//3.2测试
// int area=test.method(); 用area变量接收一下,否则不能打印面积
// System.out.println("面积为:"+area);
//3.2方法二:
// System.out.println(test.method());
//3.3用m和n
// test.method(12,10);
// System.out.println(test.method(12,10));
int area=test.method(12,10);
System.out.println("面积为:"+area);
}
//3.1
// public void method() {
// for (int i = 0; i < 10; i++) {
// for (int j = 0; j < 8; j++) {
// System.out.print("* ");
// }
// System.out.println();
// }
// }
//3.2
// public int method() {
// for (int i = 0; i < 10; i++) {
// for (int j = 0; j < 8; j++) {
// System.out.print("* ");
// }
// System.out.println();
// }
// return 10*8;
// }
//3.3
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;
}
}