A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. /**
  2. * 编写程序接收键盘输入的5个数,装入一个数组,并找出其最大数和最小数。
  3. *
  4. * @author zyy
  5. *
  6. */
  7. public class Test6 {
  8.         public static void main(String[] args) {
  9.                 // 声明一个存储五个数字的整数数组
  10.                 System.out.println("请输入五个整数(以回车符作为一个整数的结束):");
  11.                 Integer[] console = new Integer[5];
  12.                 for (int i = 0; i < console.length; i++) {
  13.                         console[i] = getConsole();
  14.                 }
  15.                 System.out.println("\n数组中最大值是:" + getMaxValue(console) + "\n数组中最小值是:"
  16.                                 + getMinValue(console));
  17.         }

  18.         /**
  19.          * 获取整数数组里面的最小值
  20.          *
  21.          * @param console
  22.          */
  23.         private static Integer getMinValue(Integer[] console) {
  24.                 if (null == console) {
  25.                         throw new IllegalArgumentException("console is not allowed null");
  26.                 }
  27.                 int min = console[0];
  28.                 for (int i = 1; i < console.length; i++) {
  29.                         if (console[i] < min) {
  30.                                 min = console[i];
  31.                         }
  32.                 }
  33.                 return min;
  34.         }

  35.         /**
  36.          * 获取整数数组里面的最大值
  37.          *
  38.          * @param console
  39.          */
  40.         private static Integer getMaxValue(Integer[] console) {
  41.                 if (null == console) {
  42.                         throw new IllegalArgumentException("console is not allowed null");
  43.                 }
  44.                 int max = console[0];
  45.                 for (int i = 1; i < console.length; i++) {
  46.                         if (console[i] > max) {
  47.                                 max = console[i];
  48.                         }
  49.                 }

  50.                 return max;
  51.         }

  52.         /**
  53.          * 获取控制台输入的整数,这部分没有进行异常处理,只考虑了安全的情况下
  54.          *
  55.          * @return 返回控制台获取的整数
  56.          */
  57.         public static Integer getConsole() {
  58.                 return new Scanner(System.in).nextInt();
  59.         }
  60. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马