7.9(找出最小元素)使用下面的方法头编写一个方法,求出一个整数数组中的最小元素: public static double min(double[] array) 编写测试程序,提示用户输入是个数字,调用这个方法返回最小值,并显示该最小值。下面是该程序的运行示例: Enter 10 numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2 The minimum number is 1.5 7.9(Find the smallest element)Use the following method header to write a method to find the smallest element in an integer array: public static double min(double[] array) Write a test program, prompt the user to input a number, call this method to return the minimum value, and display the minimum value. The following is an example of how to run the program: Enter 10 numbers: 1.9 2.5 3.7 2 1.5 6 3 4 5 2 The minimum number is 1.5
参考代码:
package chapter07;import java.util.Scanner;publicclassCode_09{publicstaticvoidmain(String[] args){double[] array =newdouble[10];
Scanner input =newScanner(System.in);
System.out.print("Enter 10 numbers: ");for(int i =0;i <10;i++)
array[i]= input.nextDouble();
System.out.println("The minimum number is "+min(array));}publicstaticdoublemin(double[] array){double min = array[0];for(int i =1;i < array.length;i++)if(min > array[i])
min = array[i];return min;}}
结果显示:
Enter 10 numbers:1.92.53.721.563452
The minimum number is 1.5
Process finished with exit code 0