最小数组

时间:2015-02-12 13:36:06

标签: java arrays sorting

我对我应该从哪里回来感到困惑:

public static double min(double[] array) {
    double[] tenDoubles = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter " + tenDoubles.length + " numbers: ");
    for (int i = 0; i < tenDoubles.length; i++){
        tenDoubles[i] = input.nextDouble();
    }

    for (int j = 0; j < tenDoubles.length; j++) {
        double currentMin = tenDoubles[j];
        double currentMinIndex = j;
        for (int k = j; k < tenDoubles.length; k++) {
            if (currentMin > tenDoubles[k]) {
                currentMin = tenDoubles[k];
                currentMinIndex = k;
            }
        }
    }
}

如何从此方法返回值并打印出用户输入的最小值?

4 个答案:

答案 0 :(得分:2)

通过使用java api,您可以找到最小数量
要查找最小值,请将数组tenDoubles转换为List,然后使用Collections.min()方法找到最小值。

在您的代码中,我已完成修改以解决问题
我修改了你的代码,你正在用户的两个地方输入 1.在min()方法中,您使用扫描仪进行输入 2.您没有将double[] array作为参数传递给min()方法。

将您的数组作为参数传递给min()方法它将找出最小值和传递结果。

public class Main {

    public static void main(String[] args) {

        double[] tenDoubles = new double[10];
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("Enter " + tenDoubles.length + " numbers: ");

        for (int i = 0; i < tenDoubles.length; i++){
            tenDoubles[i] = input.nextDouble();
        }
        System.out.println(min(tenDoubles));
    }

    public static double min(double[] tenDoubles) {

        double currentMin=Integer.MAX_VALUE;
        for (int j = 0; j < tenDoubles.length; j++) {
            if (tenDoubles[j]< currentMin) {
                currentMin = tenDoubles[j];
            }
        }
        return currentMin;
    }
}

答案 1 :(得分:1)

您的示例需要重组。首先将值读入数组,然后将此数组作为参数传递给min函数。要返回最小值,请添加

return currentMinIndex;

最后使用System.out.println打印返回的值。

你的min函数看起来像

public static double min(double[] array) {
    for (int j = 0; j < array.length; j++) {
        double currentMin = array[j];
        double currentMinIndex = j;
        for (int k = j; k < array.length; k++) {
            if (currentMin > array[k]) {
                currentMin = array[k];
                currentMinIndex = k;
            }
        }
    }
    return currentMinIndex;
}

虽然没有必要两次完成数组。

答案 2 :(得分:1)

改进代码:

public static double min() {
  double[] tenDoubles = new double[10];// = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  java.util.Scanner input = new java.util.Scanner(System.in);
  System.out.print("Enter " + tenDoubles.length + " numbers: ");
  for (int i = 0; i < tenDoubles.length; i++){
      tenDoubles[i] = input.nextDouble();
}

  double currentMin = tenDoubles[0];
  for (int j = 1; j < tenDoubles.length; j++) {
       if (currentMin > tenDoubles[j]) {
           currentMin = tenDoubles[j];
       }        
  }

  return currentMin;}

答案 3 :(得分:0)

您只需跟踪最小值(您正在进行的操作)并将其与阵列中的其他元素进行比较。如果下一个元素较少,则更改值。

// This will hold what the minimum value is
double currentMin = 0;

// The current value from the tenDoubles array
double currentValue;

// loop through each element of the array
for (int j = 0; j < tenDoubles.length; j++) {
    // get the current value from the array
    currentValue = tenDoubles[j];

    // if this is the first element, then just set the minimum to this value
    if (j ==0) {
        // Set the value as the minimum
        currentMin = currentValue;
    }else {
        // if the current value is less that what is already saved as the minimum,
        // set it as the new minimum
        if (currentValue < currentMin) {
            currentMin = currentValue;
        }
    }
}

System.out.println("Minimum value: " + currentMin);

return currentMin;

<强>更新

如果您不必使用for循环,只需执行以下操作即可将其缩短:

public static double min(double[] array) {
    Arrays.sort(array);

    return array[0];
}