a- package test;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Test4 {
- /**
- * 需求:鍵盤录入多个数据,以0结束,最后输出最大值
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //创建键盘输入对象
- Scanner sc = new Scanner(System.in);
- //创建集合存储数据
- ArrayList<Integer> al = new ArrayList<Integer>();
- //录入数据
- int num = -1;
- System.out.println("请输入数据,输入 0 时结束:");
- while((num = sc.nextInt())!=0){
- al.add(num);
- }
- //获取最大值
- int max = getMax(al);
- System.out.println("最大值为:"+max);
- }
- private static int getMax(ArrayList<Integer> al) {
- // TODO Auto-generated method stub
- int max = -1;
- for(int i:al){
- max = i>max?(max = i):(max);
- }
- return max;
- }
- }
复制代码
|
|