Ich habe zwei Probleme mit meinem Code. 1) Es gibt mir nicht den richtigen Durchschnittswert, wenn ich meiner Arraylist Werte hinzufüge, also weiß ich, dass mein Zustand in meiner for-Schleife ausgeschaltet ist und 2) Es zeigt nicht alle Zahlen in meiner arrayList an. Es zeigt speziell nicht die 0. ganze Zahl.ArrayList Berechne Durchschnitt durch Fallenlassen Kleinste
Hier ist mein Code:
public class CalcAvgDropSmallest {
public static void main(String[] args) {
int lowNum = 0; // # of the lowest numbers to be dropped
double average; // calcuates the mean of the sum and the lowest numbers dropped
ArrayList<Double> inputs= getALInfo();
lowNum = getLowestnum();
average = calculateAvg(inputs, lowNum);
getAvg(inputs, lowNum, average);
}
public static ArrayList<Double> getALInfo() {
ArrayList<Double> inputs = new ArrayList<Double>();
// Read Inputs
Scanner in = new Scanner(System.in);
System.out.println("Please enter 5 - 10 integers, Q to quit: ");
Double vals = in.nextDouble();
while (in.hasNextDouble())
{
inputs.add(in.nextDouble());
}
return inputs;
}
public static int getLowestnum() {
int lowNum = 0;
// Reads Input value for # of lowest values dropped
System.out.println("How many of the lowest values should be dropped?");
Scanner in = new Scanner(System.in);
lowNum = in.nextInt();
return lowNum;
}
public static double calculateAvg(ArrayList<Double> inputs, int lowNum) {
double sum = 0;
double average = 0;
int i = 0;
// Calcuates the average of the array list with the lowest numbers dropped
for (i = 0; i < inputs.size(); i++)
{
if (inputs.get(i) > lowNum) {
sum = sum + inputs.get(i);
}
}
average = (sum/inputs.size());
return average;
}
public static void getAvg(ArrayList<Double> inputs,int n, double average) {
// It's adding all the values and dividing by the size of it, which is a problem
// Also, it's not showing the 0th integer aand just straight to the 1st integer
System.out.println("The average of the numbers " + inputs + " except the lowest " +n+ " is " +average);
}
}
Sie Wegwerfen die ersten Doppel eingegeben! –
Hier: 'Double vals = in.nextDouble();'. Tun Sie das nicht, ignorieren Sie diesen ersten Eintrag nicht. Sie geben Vals ein, aber legen Sie es nie in Ihre ArrayList. –
Danke! Ich habe es jetzt behoben. –