public class Test{
public static void main(String[] a){
double[] arr = {1,2,3,4.5,5,6,8};
double find = 4.3,result = 0;
double flag = Math.abs(arr[0]-find);
for(int i =0;i<arr.length;i++){
double d = Math.abs(arr[i]-find);
if(d<=flag){
flag = d;
result = arr[i];
}
}
System.out.println("最接近"+find+"的数组中数值是"+result);
}
} |