Java中数组的最小值和最大值 [英] min- and max value of an array in Java
本文介绍了Java中数组的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取数组的最小值和最大值.问题是我不确定 for循环
中到底发生了什么.我们用 int
创建一个 for循环
.然后出现if语句以及我的问题: numbers [i]
应该是什么意思??我认为 numbers
已经指定了大小(9,通过初始化 {1,2,3,...,9} ).我们如何将其更改为 [i]
,我代表什么?
I'm trying to get the minimum and maximum value of an array. The problem is that I'm not sure what exactly happens in the for loop
. We create a for loop
with an int
. Then comes the if statement and also my question: What should numbers[i]
mean? I thought that numbers
already has specified size (9, by initializing { 1,2,3,...,9}). How can we change that to [i]
and what does that i stand for?
public class Array {
public static void main(String[] args) {
int numbers[] = { 92, -108, 2, 120, 67, 4, -81, 9, 88, 1 };
int min, max;
min = max = numbers[0];
for (int i = 1; i < 10; i++) {
if (numbers[i] < min)
min = numbers[i];
if(numbers[i] > max){
max = numbers[i];
}
}
System.out.println("min is: " + min + "; max is: " + max);
}
}
期待您的回复
推荐答案
numbers [i]
是访问数组中 ith 元素的方式.
numbers[i]
is how you access the ith element in the array.
这篇关于Java中数组的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文