return
-
返回值
-
直接终止方法
-
通常是写在方法最后的
package fangfa; public class Day07 { //设计一个比大小的方法,然后比大小 public static void main(String[] args) { int max = max(100, 100); System.out.println(max); } public static int max(int a, int b){ int result = 0; if (a > b){ result = a; }else if (a == b){ System.out.println("a==b"); return 0; //终止方法 }else { result = b; } return result;//a==b 0 } }
重载
-
同方法名,但参数不同
-
重载时一个类里面,方法名字相同,而参数不同,返回值可以相同也可以不同
-
参数个数不同,类型不同,排序不同
三个数比大小
package fangfa; import java.util.Scanner; public class Day07 { //设计一个比大小的方法,然后比大小 public static void main(String[] args) { System.out.println("please input a="); System.out.println("please input b="); System.out.println("please input c="); //键盘随机输入三个数 Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); int c=scanner.nextInt(); scanner.close(); int max; max=a; int min; int mid; if(b>=a) {if(b>=c){ if(a>=c){ max=b; //c=<a=<b mid=a; min=c; System.out.println(min+"<="+mid+"<="+max); } else { max=b; //a<c=<b mid=c; min=a; System.out.println(min+"<"+mid+"<="+max); } } else{ //b>a,b不大于c max=c; //a=<b<c mid=b; min=a; System.out.println(min+"<="+mid+"<"+max); } } else { //a>b if(a>=c) { if(b>=c){ min=c; //c=<b<a mid=b; max=a; System.out.println(min+"<="+mid+"<"+max); } else{ //b<c<=a min=b; mid=c; max=a; System.out.println(min+"<"+mid+"<="+max); } } //b<a<c else{ min=b; mid=a; max=c; System.out.println(min+"<"+mid+"<"+max); } } } }