溫馨提示×

java怎么獲取數組中最大的值

小億
129
2023-08-16 00:28:22
欄目: 編程語言

你可以使用循環遍歷數組,然后比較每個元素的值來找到最大值。

以下是一個使用Java的示例代碼:

public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 8};
int max = array[0]; // 假設第一個元素為最大值
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
System.out.println("數組中最大的值為: " + max);
}
}

運行以上代碼,將輸出:

數組中最大的值為: 9

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女