我必须接受5个名字和每个缺席的次数

时间:2013-11-27 13:51:15

标签: java arrays position max

我能够找到最大但不是它在阵列中的位置来确定它是谁,HELP ????? 感谢!!!

public static void main(String[] args) {
    // TODO code application logic here

    String name[]=new String[5];
    String absent[]=new String[5];

    for(int i=0; i<=4; i++){

       name[i]=JOptionPane.showInputDialog("Enter name");
       absent[i]=JOptionPane.showInputDialog("Enter absent total");}
       int absentint[] =new int[5];

       for(int i=0; i<=4; i++){
           absentint[i]=Integer.parseInt(absent[i]);
       }

       int j=0;
       int sum=0;
       int pos=0;
       while(j<=4){
          sum=sum+absentint[j];
          j++;
       }

       System.out.println(sum/absentint.length);
       int max=0;
       for(int l=0; l<=4; l++){
          if(absentint[l]>max){         
             max=absentint[l];
           }
        }

    System.out.println("Max= "+max);
    System.out.println("Position is "+ pos);
    System.out.println(Arrays.toString(absentint));
    int x= Collections.indexOfSubList(Arrays.asList(absentint), Arrays.asList(max));

1 个答案:

答案 0 :(得分:0)

看起来这是找到max:

的代码
int max=0;
for(int l=0; l<=4; l++){
    if(absentint[l]>max)
    {
     max=absentint[l];
    }
}

所以你只需要保存索引以及值:

int max=0, index=0;
for(int l=0; l<=4; l++){
    if(absentint[l]>max)
    {
     max=absentint[l];
     index =l;
    }
}