课堂练习四
源代码:
package test1; import java.util.Scanner; class Amax{ int largest(int list[],int length) { int max=list[0],i; for(i=0;(i<length);i++) { if(list[i]>max) {max =list[i];} } return max; } } public class Max { public static void main(String[] args) { Amax a=new Amax(); int[] list =new int[100]; int length; Scanner input =new Scanner(System.in); System.out.println("请输入数的个数:"); length = input.nextInt(); if(length<=0) { System.out.println("未输入数!"); } else { System.out.println("请输入数:"); for(int i=0;i<length;i++) { list[i]=input.nextInt(); } System.out.println("Max ="+a.largest(list,length)); } } }
测试数据:
①有序:
②没数字:
③重复:
④负数:
⑤有零:
⑥正负0: