程序源代码:
package max;
import java.util.Scanner;
class Max1{
int Largest(int list[],int length)
{
int i,max=list[0];
for(i=0;i<(length-1);i++)
{
if(list[i]>max)
{
max=list[i];
}
}
return max;
}
}
public class Max {
public static void main(String[] args) {
Max1 m=new Max1();
int list[]=new int[5];
int i,length=5;
int max;
Scanner sc=new Scanner(System.in);
System.out.println("请输入数的个数上限为5");
length=sc.nextInt();
if(length==0||length>5)
{
System.out.println("输入不正确或超出范围");
}
else
{
System.out.println("请输入一组整数");
for(i=0;i<length;i++)
{
list[i]=sc.nextInt();
}
max=m.Largest(list, length);
System.out.println("最大值为"+max);
}
}
}
结果截图:1:相同数2:有正有负3:乱序4:全负数降序5:全正数降序6:用户输入不正确7:升序