java怎么输出一个数组中的最大值

来源:百度知道 编辑:UC知道 时间:2024/05/30 09:59:30
怎么输出一个数组中的最大值

楼上的你那么写 如果数组中含有负数的话 就貌似不行了哦.初始化错鸟
要这样:
class Test
{
public static void main(String[] args)
{
int array[]={-5,-4,-3,-2,-1,0,1,2,3,4,5};
int max=array[0];
for(int i=0;i<array.length;i++){

if(array[i]>max){
max=array[i];
}
}
System.out.println("max num:"+max);
}

}

假设你的数组是array[],那么
int max = array[0];
for(int i = 1; i < array.length; i++){
if(max < array[i]) max = array[i];
}
那么max现在就是数组中的最大值啦

package com;

public class CountMax {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {3,1,6,5,3,656,0};
int max = arr[0];
for(int i=0;i<arr.length;i++) {
if(max<arr[i]) {
max = arr[i];
}
}

System.out.println("最大值是:"+max);
//我已经测试过了
}