此处实现了数组为空的情况,不过把方法定义成了Integer,要不不会返回null。而且用Arraylist类实现了长度可变,并用nextInt接入值给了Integer。所以不能实现用false等字母退出循环。

package
课堂; import java.util.ArrayList; import java.util.Scanner; public class Findthemaximum { @SuppressWarnings({ "resource", "unchecked", "rawtypes" }) public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList list = new ArrayList();//变长数组,用于存放输入的数。 System.out.println("请输入任意个整数。"); int length = 0;//记录数组长度。 while(true)//把输入的数字存于list,当输入00时,退出。 { Integer value = sc.nextInt(); if(value == 99) { break; } length++; list.add(value); } Integer []list1 = new Integer[length];//把list中的元素复制到list中。 for(int i = 0;i < length;i++) { list1[i] = (Integer) list.get(i); } System.out.println("所输入的几个数最大值为" + Largest(list1,length)); } static Integer Largest(Integer []list,int length) { Integer max = null; if(length == 0)//如果数组全为空或长度为零,返回空。 {//因为数组变长,所以不存在中间数出现空而导致返回空的 max = (Integer) null; return max; } else { max = list[0]; } for(int i = 0;i < length;i++) { if(list[i] > max) { max = list[i]; } } return max; } }
全为整数。有正有负有重复。
全为负数。正数重复无规律。
有正数绝对值大有相反数。
什么也不输入。

 

posted on 2016-03-21 17:21  消失。  阅读(126)  评论(0编辑  收藏  举报