无参数且无返回值时
public static void hello(){ //首先定义一个方法,用来和老师打招呼
System.out.println("老师好!");
}
public static void main(String[] args) {
hello(); //调用方法
}
结果是
老师好!
无参数但有返回值时
public static void main(String[] args) {
int p= calcAvg(); //调用方法获取返回值
System.out.println("平均成绩为:" + p);
}
public static int calcAvg() { // 定义一个返回值为int类型的方法
int i = 59;//定义两个整数i,j 并赋值
int j = 61;
int p = (i+j)/2; //定义字母p为平均数,并且列出计算平均值的公式
return p; // 使用return返回值
结果是
平均成绩为:60