找到最小的数字的索引 [英] find smallest number's index

查看:71
本文介绍了找到最小的数字的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它返回数组中的第二个元素,而不是最小数字的索引
我已经拿了大小和所有东西,这只是方法

It's returning the second element in the array instead of the smallest number's index
I already took the size and all that stuff, this is just the method

public static int FindSmallest (int [] arr1){//start method

    int index = arr1[0];

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

        if (arr1[i] > index ){
            index = arr1[i];
        }

        return index ;
    }

    return 0;

}//end method

推荐答案

public static int FindSmallest (int [] arr1) {
       int index = 0;
       int min = arr1[index];

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

           if (arr1[i] < min) {
               min = arr1[i];
               index = i;
           }
           ...
       }
       return index;
}

这篇关于找到最小的数字的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆