求一段数的最大值,次大值

朴灿烈づ我的快乐病毒、 2022-05-24 06:40 230阅读 0赞
  1. package com.bj58.pn.sjjg;
  2. public class NumberDemo {
  3. public static void printFirSec(int[] a) {
  4. int n = a.length;
  5. int max = a[0];
  6. int sec = a[0];
  7. if (n < 2) {
  8. System.out.println("不足两个元素");
  9. return;
  10. }
  11. for (int i = 1; i < n; i++) {
  12. if (max > a[i]) {
  13. if (sec < a[i])
  14. sec = a[i];
  15. } else {
  16. sec = max;
  17. max = a[i];
  18. }
  19. }
  20. System.out.println("MAX"+max);
  21. System.out.println("SEC"+sec);
  22. }
  23. public static void main(String[] args) {
  24. int[] a = {1,2,4,3,2,5,6,9,8};
  25. printFirSec(a);
  26. }
  27. }

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读