-3
有一个程序计算数组中的一组内置数字之间的最小差异(因为在这种情况下,最小差异是3,因为数字的最小差异在4和1之间) 。我想程序中值使用for循环设定值进入到6量这是低于当前的代码,而不是采取从用户:存储值的用户输入以显示最小差异
import java.util.*;
public class Mindifference {
Scanner numinput= new Scanner(System.in);
public static void main(String[] args)
{
int[] a = new int[] {4, 9, 1, 32, 13};
Arrays.sort(a);
int minDiff = a[1]-a[0];
for (int i = 2 ; i != a.length ; i++) {
minDiff = Math.min(minDiff, a[i]-a[i-1]);
}
System.out.println(minDiff);
}
}
什么是输出?你期望输出什么? – Bathsheba
我希望输出是最小差值的计算值。因此与当前代码所做的一样,它会打印出3,因为这些数字之间的最小最小差异在1和4之间。 – megasxrl