Java 如何在数组中删除最小值?
基本上,我需要获得5个等级的用户输入,将它们相加,得到平均值。在得到平均值之前,我还需要以某种方式从该数组中删除最低值 最重要的是,我需要以某种方式循环所有这些,以便它在结尾询问“如果你想重做这个东西,请键入true如果你完成了,请键入false”,但我想先找出如何删除最低值 顺便说一句,我还没有“正式”学会如何做数组或循环,所以所有这些毫无意义的东西都是自学的Java 如何在数组中删除最小值?,java,loops,Java,Loops,基本上,我需要获得5个等级的用户输入,将它们相加,得到平均值。在得到平均值之前,我还需要以某种方式从该数组中删除最低值 最重要的是,我需要以某种方式循环所有这些,以便它在结尾询问“如果你想重做这个东西,请键入true如果你完成了,请键入false”,但我想先找出如何删除最低值 顺便说一句,我还没有“正式”学会如何做数组或循环,所以所有这些毫无意义的东西都是自学的 import java.util.Scanner; import java.util.Arrays; class calculate
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);
}
}
}
import java.util.Scanner;
导入java.util.array;
类计算器{
公共静态void main(字符串参数[]){
int[]等级=新的int[5];//分配等级的数组
整数和=0;
整数平均;
扫描仪键盘=新扫描仪(System.in);
//此循环用于接收分配等级的用户输入
System.out.println(“请输入您的5个作业分数:”);
对于(int fcount=0;fcount而不是将其从数组中删除,我将跟踪最小值是哪个,然后从总和中减去它
如果您在跟踪最小值时遇到问题,请告诉我。我不会将其从数组中删除,而是跟踪最小值,然后从总和中减去它
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
}
}
如果跟踪最小值有困难,请告诉我。公共静态void main(字符串args[]){
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
}
}
int[]等级=新的int[5];//分配等级的数组
整数和=0;
整数平均;
字符串continue=“false”;
扫描仪键盘=新扫描仪(System.in);
执行{//开始程序循环
//此循环用于接收分配等级的用户输入
System.out.println(“请输入您的5个作业分数:”);
对于(int-fcount=0;fcountpublicstaticvoidmain(字符串args[]){
int[]等级=新的int[5];//分配等级的数组
整数和=0;
整数平均;
字符串continue=“false”;
扫描仪键盘=新扫描仪(System.in);
执行{//开始程序循环
//此循环用于接收分配等级的用户输入
System.out.println(“请输入您的5个作业分数:”);
对于(int fcount=0;fcount如果要实际删除该值,则应使用类似于arraylist的内容,因为正如其他人所提到的:数组是固定长度的。
如果使用arraylist,您可以
Arrays.sort(yourArrayList)
而您的最小值应该是第一个值。如果您想实际删除该值,您应该使用类似于arraylist的东西,因为正如其他人所提到的:数组是固定长度的。
如果你用了arraylist你可以
Arrays.sort(yourArrayList)
你的最小值应该是第一个。注意,在Java中,你实际上不能从数组中删除元素,因为数组是固定长度的。你可以将所有其他值复制到另一个数组中,或者使用列表来代替(但这对Java初学者来说有点牵强)。是的,谢谢你指出这一点(我只是想用他的措辞)别忘了,一旦你减去最小值,求平均值就会除以“grades.length-1”,因为你已经去掉了一个值。好吧,你们说的对我来说非常陌生。你如何跟踪最小值?@VexSilver(sum minGrade)/(grades.length-1)
就是这个建议。其中,sum
是您计算的5个等级的总和,minGrade
是您看到的最低等级。这只是数学,而不是代码思维。请注意,您不能在Java中实际删除数组中的元素,因为数组是固定长度的。您可以将所有其他值复制到另一个数组,或者使用<代码>列表
(但对于java初学者来说,我想这有点牵强)。是的,谢谢你指出这一点(我只是想用他的措辞)别忘了,一旦你减去最小值,求平均值就会除以“grades.length-1”,因为你已经去掉了一个值。好吧,你们说的对我来说非常陌生。你如何跟踪最小值?@VexSilver(sum minGrade)/(grades.length-1)
就是这个建议。其中,sum
是你计算的5个等级的总和,minGrade
是你看到的最低等级。这只是数学,不是用代码思考……最好选择正式的风格,没有任何个人参考;)为什么有那么多的问题涉及到计算作业的平均值!!!@JoshM因为有那么多的教程和老师使用aas简介;)@JoshM-因为这是一个学习的好例子(面向对象之前)Java基础知识。@VexSilver变得粗鲁在这里没有帮助。Alexei也没有错,你应该始终保持你的问题中立,在这里保持正式的风格…最好选择正式的风格,没有任何个人参考;)为什么有那么多的问题涉及到计算作业的平均值!!!@JoshM因为有那么多的教程和老师使用aas简介;)@JoshM-因为这是一个学习的好例子(面向对象之前)Java基础知识。@VexSilver变得粗鲁在这里没有帮助。Alexei也没有错,你应该始终保持你的问题中立,在这里保持正式的风格。这实际上给了一个好主意,因为整除4是固定在5。谢谢continue
是一个特殊的关键字(不能将它用作变量名)您不应该将字符串与==
进行比较。这实际上是一个很好的主意,因为它被固定为5。谢谢continue
是一个特殊的关键字(不能将其用作变量名),您不应该将字符串与=
进行比较。