gpt4 book ai didi

java - 在 int 数组中,如何返回对应于最低值的索引?

转载 作者:行者123 更新时间:2023-11-29 04:06:57 25 4
gpt4 key购买 nike

<分区>

我已经设法用我的“最低”变量找到数组中的最低值,但我正在寻找与数组中最低值相对应的索引。有什么想法吗?

public class Marathon {

public static void main(String[] args) {

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]);
}
lowesttime(names, times);
}

public static void lowesttime(String names[], int times[]) {
int lowest;

lowest = times[0];
for (int i = 1; i < times.length; i++) {
if (times[i] < lowest) {
lowest = times[i];
}
}
System.out.println(lowest);

// to access arrays names[?], times[?}
// System.out.println(names[lowest] + ": " + times[lowest]);
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com