我是靠谱客的博主 等待薯片,这篇文章主要介绍第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)第七章第十题(找出最小元素的下标)(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
最后
以上就是等待薯片最近收集整理的关于第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)的全部内容,更多相关第七章第十题(找出最小元素的下标)(Find内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复