第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)
程序员文章站
2022-06-26 18:16:10
第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)7.10(找出最小元素的下标)编写一个方法,求出整数数组中最小元素的下标。如果这样的元素个数大于1,则返回最小的下标。使用下面的方法头:public static int indexOfSmallElement(double[] array)编写一个测试程序,提示yoghurt输入10个数字,调用这个方法,返回最小元素的下标,然后显示这个下标值。7.10(Find the su...
第七章第十题(找出最小元素的下标)(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
本文地址:https://blog.csdn.net/jxh1025_/article/details/109267572
下一篇: java二维码生成器