1.例4.4内置函数
public class cjava { public static void main(String[] args) { int i=10,j=20,k=30,m; m=max(i,j,k); System.out.println("max="+m); } final static int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a; } }
例4.5重载函数(参数类型)
import java.util.Scanner; public class cjava { public static void main(String[] args) { int i1,i2,i3,i; @SuppressWarnings("resource") Scanner a=new Scanner(System.in); i1=a.nextInt(); i2=a.nextInt(); i3=a.nextInt(); i=max(i1,i2,i3); System.out.println("max="+i); double d1,d2,d3,d; d1=a.nextDouble(); d2=a.nextDouble(); d3=a.nextDouble(); d=max(d1,d2,d3); System.out.println("max="+d); long g1,g2,g3,g; g1=a.nextLong(); g2=a.nextLong(); g3=a.nextLong(); g=max(g1,g2,g3); System.out.println("max="+g); } static int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a; } static double max(double a,double b,double c) { if(b>a) a=b; if(c>a) a=c; return a; } static long max(long a,long b,long c) { if(b>a) a=b; if(c>a) a=c; return a; } }
例4.6函数重载(参数个数)
public class cjava { public static void main(String[] args) { int a=8,b=-12,c=27; System.out.println("max(a,b,c)="+max(a,b,c)); System.out.println("max(a,b)="+max(a,b)); } static int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a; } static int max(int a,int b) { if(b>a) a=b; return a; } }
2.没问题。
3.明天继续写例题。