如何在数组中删除最小值?

Vex*_*ver 0 java loops

基本上,我需要获得5个等级的用户输入,将它们加在一起并获得平均值.在获得平均值之前,我还需要以某种方式从该数组中删除最低值.

最重要的是,我需要以某种方式循环所有这些,以便它在结束时询问"如果你想重做这个东西,键入true,如果你完成了就输入false"但我想知道如何首先删除最低值

顺便说一下,我还没有"正式"学会如何做数组或循环,所以所有这些都没有意义是自学.

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

class calculateGrades{
public static void main(String args[]){

    int[] grades=new int[5]; //Array for assignment grades
    int sum=0;
    int average;

    Scanner keyboard=new Scanner(System.in);

    //This loop is to take in the user input for assignment grade 
    System.out.println("please enter your 5 assignment grades: ");
    for (int fcount=0;fcount<grades.length;fcount++){
        grades[fcount]=keyboard.nextInt();
        }
    //After this loop is done, all the grades are now placed in the 
    //grades array.

    //Find the minimum value in the array.
    Arrays.sort(grades);
    int mn = grades[0];

    //This loop is to calculate the sum of the inputed array.
    for(int counter=0;counter<grades.length;counter++){
        sum=sum+grades[counter];

        }
    //Now that this array calculated the sum of the array we find the average
    average = (sum - mn) /4;

    System.out.println(average);
    System.out.println(mn);

}


}

}
Run Code Online (Sandbox Code Playgroud)

所以有人可以帮助我吗?

xp5*_*500 5

我不会将其从数组中删除,而是跟踪最小值,然后从总和中减去它.

如果您无法跟踪最小值,请告诉我.

  • 请注意,您实际上不能在Java中将元素从数组中删除,因为数组是固定长度的.你可以将所有其他值复制到另一个数组,或者使用`List`代替(但对于java初学者来说,我觉得有点牵强). (6认同)
  • 不要忘记,一旦你减去了最低值,找到平均值就会除以'grades.length-1',因为你删除了一个值. (3认同)