JAVA今日2021.8.12
JAVA 方法
格式
修饰符 返回值类型 方法名(参数类型,参数名){
...
方法体
...
return 返回值;//如果返回值类型为void,可以不用返回
}
形式参数:用于定义
实际参数:实际调用的
方法体:包含具体语句,定义该方法的功能
当方法返回一个值的时候;方法通常会被当作一个值,例:
int larger = max(10,20);
如果方法返回值是void,方法调用的一定是语句,例:
System.out.println("Hello,World!");
mt02
package JAVASE.method;
public class mt02 {
public static void main(String[] args) {
int m = m(1, 2);
System.out.println(m);
}
//比大小
public static int m(int a,int b){
int r = 0;//初始化
if (a==b){
System.out.println("a==b");
return 0;//终止方法
}
if (a>b){
r=a;
}else {
r=b;
}
return r;
}
}
mtt00
package JAVASE.method;
import java.util.Scanner;
public class mtt00 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = 0;
int y = 0;
System.out.println("请输入x:");
if (scanner.hasNextInt()){
x = scanner.nextInt();
}
System.out.println("请输入y:");
if (scanner.hasNextInt()){
y = scanner.nextInt();
}
int xn = xn(x, y);
System.out.println("较小的值为:"+xn);
scanner.close();
}
public static int xn(int a,int b){
int s=0;
if (a==b){
System.out.println("a=b");
return 0;
}
if (a<b){
s=a;
}else {
s=b;
}
return s;
}
}
2021.8.12

浙公网安备 33010602011771号