将数组中最小的数输出

【题目要求】:编写Java程序,将数组中最小的数输出。

【题目分析】

  • 思想一:采用冒泡排序由小到大,则最小的数在首位,输出首位即可。
  • 思想二:调用方法
  • 思想三:for循环嵌套if条件句,输出最小值

【编程实现】

思想一:

 
  1. public class Test2 {

  2. public static void main(String[] args) {

  3. int arr[] = {12, 34, 13, 24, 999, 9};

  4. for (int i = 0; i < arr.length - 1; i++) {

  5. for (int j = 0; j < arr.length - 1 - i; j++) {

  6. if (arr[j] > arr[j + 1]) {

  7. int temp;

    扫描二维码关注公众号,回复: 10890774 查看本文章
  8. temp = arr[j];

  9. arr[j] = arr[j + 1];

  10. arr[j + 1] = temp;

  11. }

  12. }

  13. }

  14. System.out.println("数组中的最小数为:" + arr[0]);

  15. }

  16. }

 思想二:

 
  1. Arrays.sort(arr);

  2. System.out.println("数组中的最小数为:" +arr[0]);

思想三:

 
  1. int min=arr[0];

  2. for(int i=1;i<arr.length;i++){

  3. if(arr[i]<min){

  4. min=arr[i];

  5. }

  6. }

  7. System.out.println("数组中的最小数为:" +min);

发布了60 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_37876078/article/details/105584003