近期梳理了学习大纲耽误几天。后续不断更新
方法与方法调用1:无参数无返回值
代码:
public class Fangfa1{//无参数无返回值!!!!
public static void main(String[] args){
cook();//调用方法
}
//创建方法 名为cook
public static void cook(){//无返回值用void,,static是静态
String cook1 = "洗菜";//创建
String cook2 = "炒菜";
String cook = cook1+cook2;
System.out.println(cook);
}}
结果:
方法2 :有参数无返回值
代码“:
public class Fangfa2{//定义类名 有参数无返回值
public static void main(String[] args){
suanshu(5,10);//调用方法并赋值
}
public static void suanshu(int a , int b){//创建方法参数
int sum = a + b;
System.out.println(sum);
}
}
方法3:有反回值无参数
代码
public class Fangfa3{// 有反回值无参数
//有返回值模式 public static 返回值类型 方法名(){
// 方法体(循环体)
// }
public static void main(String[] args){
String d = st();
System.out.println(d);
}
public static String st(){
String a ="love";
String b ="miss";
String c = a + b;
return c;//返回值【哪里调用返回哪里】
}
}
方法4:有返回值有参数
代码:
public class Fangfa4{//定义类
public static void main(String[] args){
int c = chengchu(3,5);//调用
int d = c;
System.out.println(d);
}
public static int chengchu(int a,int b){
int c = a * b;
return c;//返回值
}}
void 是无返回值