基本上,我需要获得5个等级的用户输入,将它们加在一起并获得平均值。在获得平均值之前,我还需要以某种方式从该数组中删除最低值。
最重要的是,我需要以某种方式循环所有这样,以便它在结束时询问“如果你想要重做这个东西,键入true,如果你完成了,则输入false”但我想知道如何首先删除最低值< / p>
顺便说一下,我还没有“正式”学会如何做数组或循环,所以所有这些都没有意义是自学。
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);
}
}
}
那么有人可以帮助我吗?
答案 0 :(得分:5)
不是将它从数组中删除,而是跟踪最小值,然后从总和中减去它。
如果您无法跟踪最小值,请告诉我。
答案 1 :(得分:0)
public static void main(String args[]){
int[] grades=new int[5]; //Array for assignment grades
int sum=0;
int average;
string continue = "false";
Scanner keyboard=new Scanner(System.in);
do { // start of the program loop
//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.
//This loop is to calculate the sum of the inputted 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/grades.length;
System.out.println(average);
System.out.println("Type true if you want to redo this stuff type false if your done");
continue = continue.nextLine(); // takes in a string input from user
} while (continue.equalsIgnoreCase("true")); // it will loop the whole line again when continue is true
}
}
你不能从数组中删除数组元素。所以像xp500建议的那样,跟踪最小值并从总和中减去,然后将等级除以grade.length -1作为mitim所说的,或者除以4,因为它是5的固定数组。
在这种情况下,我使用do while循环来重新循环整个程序。答案 2 :(得分:0)
如果你想实际删除该值,你应该使用类似arraylist的东西,因为正如其他人所提到的:数组是固定长度。
如果你使用了arraylist,你可以
Arrays.sort(yourArrayList)
你的最低值应该是第一个。