我怎样才能打印最快的时间

时间:2014-10-07 18:10:43

标签: java

class marathon {
    public static void main (String[] arguments) {
        String[] names = {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
                "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
                "Aaron", "Kate"
        };
        int[] times = {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265};
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i] + ": " + times[i]);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

迭代次数数组并找到最小的数据:

If (times[i]<min)
    min= times[i];

然后打印出来:

System.out.println("fastest is:"+min);

答案 1 :(得分:0)

您希望在遍历所有项目时保持跟踪 - 即,当前时间是否比以前任何时间更快(更少)?

class marathon {
    public static void main (String[] arguments) {
        String[] names = {"Elena", "Thomas", "Hamilton"
                          , "Suzie", "Phil", "Matt", "Alex"
                          , "Emma", "John", "James", "Jane", "Emily"
                          , "Daniel", "Neda", "Aaron", "Kate" };
        int[] times = {341, 273, 278
                       , 329, 445, 402, 388
                       , 275, 243, 334, 412, 393
                       , 299, 343, 317, 265};
        int fastestTimeIndex = -1;
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i] + ": " + times[i]);
            if (fastestTimeIndex == -1 || times[i] < times[fastestTimeIndex]) {
                fastestTimeIndex = i;
            }
        }
        System.out.println(names[fastestTimeIndex] + ": " + times[fastestTimeIndex]);
    }
}

答案 2 :(得分:0)

首先,您必须寻找最小的时间并在阵列中获得该时间的位置。你可以实现这样的东西:

min = ∞
min_position = 0
counter = 0
for each element in array
if element < min then
min = element
min_position = counter
end if
counter = counter + 1
end for

现在你有最小的时间和数组中的时间位置。现在你只需要检查哪个值对应于另一个数组中的那个位置

名称[min_position]

告诉我你是否需要帮助! 再见