源代码: package ceshi; import java.util.Scanner; public class m { static int Largest(int list[] ,int length) { if(list==null)//检验数组是否为空 { System.out.println("数组为空"); System.exit(0); } if(length<=0) { System.out.println("长度错误"); System.exit(0); } /*************************************************************************/ int i,max; max=list[0];//初始化最大值 for(i=0;i<=(length-1);i++)//比较得出最大值 { if(list[i]>max) { max=list[i]; } } return max;//返回 } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int list[]=new int[4]; int i,length,max; length=4; /**********************************************************/ System.out.println("请输入四个整数"); for(i=0;i<=(length-1);i++)//输入数组 { list[i]=sc.nextInt(); } /*************************************************************/ max=Largest(list,length);//求最大值 System.out.println("最大值为"+max); } }
截图
![](https://images2015.cnblogs.com/blog/753691/201603/753691-20160321224839526-776020669.png)
![](https://images2015.cnblogs.com/blog/753691/201603/753691-20160321224844542-240350060.png)
![](https://images2015.cnblogs.com/blog/753691/201603/753691-20160321224852261-130854183.png)
![](https://images2015.cnblogs.com/blog/753691/201603/753691-20160321224857339-1450536994.png)
![](https://images2015.cnblogs.com/blog/753691/201603/753691-20160321224915058-791654117.png)
![](https://images2015.cnblogs.com/blog/753691/201603/753691-20160321224918933-898064844.png)