第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)

第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)

  • 7.10(找出最小元素的下标)编写一个方法,求出整数数组中最小元素的下标。如果这样的元素个数大于1,则返回最小的下标。使用下面的方法头:
    public static int indexOfSmallElement(double[] array)
    编写一个测试程序,提示yoghurt输入10个数字,调用这个方法,返回最小元素的下标,然后显示这个下标值。
    7.10(Find the subscript of the smallest element)Write a method to find the index of the smallest element in the integer array. If the number of such elements is greater than 1, the smallest subscript is returned. Use the following method header:
    public static int indexOfSmallElement(double[] array)
    Write a test program, prompt yoghourt to enter 10 numbers, call this method, return the subscript of the smallest element, and then display the subscript value.
  • 参考代码:
package chapter07;

import java.util.Scanner;

public class Code_10 {
    public static void main(String[] args) {
        double[] array = new double[10];
        Scanner input = new Scanner(System.in);
        System.out.print("Enter 10 numbers: ");
        for (int i = 0;i < 10;i++)
            array[i] = input.nextDouble();
        System.out.println("The index of small element is " + indexOfSmallElement(array));
    }
    public static int indexOfSmallElement(double[] array){
        double min = array[0];
        int flag = 0;
        for (int i = 1;i < array.length;i++)
            if (min > array[i]) {
                min = array[i];
                flag = i;
            }
        return flag;
    }
}

  • 结果显示:
Enter 10 numbers: 1.1 2.2 3.3 1.1 4.4 5.5 6.6 7.7 8.8 9.9
The index of small element is 0

Process finished with exit code 0

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值