java求最大值并且显示他在数组中的原始位置

package s1java.sg.chap8;

import java.util.Arrays;
import java.util.Scanner;

public class HelloAccp5 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] score = new int[5];
int max;
Scanner sc = new Scanner(System.in);
System.out.println("请依次输入学生的成绩:");
for (int i = 0 ; i < score.length; i++){
System.out.print("第" + (i + 1) + "位学生的成绩是:");
score [i] = sc.nextInt();

}
max = score[1];
for (int in = 0 ; in < score.length; in++){
if(score[in] > max){
max = score[in];
}

}
System.out.println("显示最大值:" + max );
Arrays.sort(score);
System.out.println("学员成绩按升序排列:");
for (int i = 0 ; i < 5; i++){
System.out.println("第" + (i + 1) +"位学生的成绩是:" + score[i]);
}
}

}

在此基础上作修改!!

实现思路就是循环判断找到最大值(和最小值),保存起来和其他的值进行比较。代码如下:
public class Test
{
public static void main(String args[])
{
int i,min,max;
int A[]={74,48,30,17,62}; // 声明整数数组A,并赋初值

min=max=A[0];
System.out.print("数组A的元素包括:");
int j =0;
int n =0 ;
for(i=0;i<A.length;i++)
{
System.out.print(A[i]+" ");
if(A[i]>max) // 判断最大值
j =i;
max=A[i];
if(A[i]<min) // 判断最小值
min=A[i];
n =i
}
System.out.println("\n数组的最大值是:"+max+".数组的位置是:"+(j+1)); // 输出最大值和最大值的位置
System.out.println("数组的最小值是:"+min+".数组的位置是:"+(n+1)); // 输出最小值
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-03-11
改个片段就行
int location = 0;
for (int in = 0 ; in < score.length; in++){
if(score[in] > max){
max = score[in];
location = in;
}
}

增加一个变量location存最大值的位置,当有大的数的时候就把location更新。
别忘了数组下边是从0开始的。本回答被提问者和网友采纳
第2个回答  2010-03-11
for(i=1;i<a.length-1;++i)
{
max=a[0];
if(max<a[i]) max=a[i];
else continue;//不要这句也可以
}
排序的算法抄点就行了