javase-方法

加法
public class Method {
public static void main(String[] args) {
//实参:大多是具体的值
int sum = add(23,5);
System.out.println("sum= " + sum);
}
//加法
//形参 a,b 大多是变量
    public static int add(int a,int b){
return a+b;
}
}

想实现某个功能,就给它写个方法

 

 

 方法中只有输出,没有返回值(return)就用void修饰

JAVA语言的方法类似于其他语言的函数

 

 

 

程序需要严谨!!!!!!!!!!!!!!!

if (a==b){

            System.out.println("a==b");
return 0; //中止方法
}

注意加上
public class Demo04 {
public static void main(String[] args) {
int max = max(39,39);
System.out.println(max);
}
//比较两个数的大小
public static int max(int a, int b){
int result = -1;
if (a==b){
System.out.println("a==b");
return 0; //中止方法
}
if (a>b){
result = a;
}else{
result = b;
}
return result;
}
}

 java只有值传递

方法重载

方法名相同,参数类型不同,返回值同与不同不影响。

 

 

java的main方法也可以传递参数 main方法里的 string[] args 是一个字符数组参数

java cmd运行时 需要完整的包路径 java com.kaung.demo

 

 

posted @ 2022-05-16 23:58  YJin加油  阅读(31)  评论(0)    收藏  举报