62,621
社区成员
发帖
与我相关
我的任务
分享
public class Test{
public static int getMaxValue(int[] x){
//bubble sort
for(int a=1;a<x.length;a++){
for(int b=x.length-1;b>=a;b--){
if(x[b-1]<x[b]){
int temp = x[b-1] ;
x[b-1] = x[b];
x[b] = temp;
}
}
}
//return max value
return x[0];
}
public static void main(String[] args){
//if you wanted the array inputed by user,you can use the class of Scaner
int[] test = {1,24,5,2,3};
int maxValue = getMaxValue(test);
System.out.println(maxValue);
}
}
2,3楼不要误导楼主,这种求N大值的问题直接遍历一遍数组就完了,而不是第一时间想到要排序,另外1楼正解
public class Test3 {
public static void main(String[] args) {
int[] colum=new int[5];
Scanner s=new Scanner(System.in);
for(int i=0;i<5;i++){
colum[i]=s.nextInt();
}
System.out.println(getMax(colum));
}
public static int getMax(int[] colum){
int max=colum[0];
for(int i:colum){
if(i>max){
max=i;
}
}
return max;
}
}