2016-07-18 10 views
2

私のコードには2つの問題があります。 1)arraylistに値を追加するときに私に正しい平均値を与えるわけではないので、私のforループの条件がオフで、2)arrayList内のすべての数値を表示していないことがわかります。特に0番目の整数は表示されません。ここでArrayList小数点で平均を計算する

が私のコードです:

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); 
    } 
} 
+2

を完了するために! –

+0

ここに 'Double vals = in.nextDouble();'があります。しないでください、その最初のエントリを無視しないでください。あなたは値を入れますが、ArrayListに入れないでください。 –

+0

ありがとう!私は今それを修正した。 –

答えて

4

まず、(代わりにArrayList具体的なタイプの)Listインターフェイスにプログラムしてください。次に、読み取った各値をListに追加するようにしてください。次に、終端値を読み、それを破棄してください。さもなければそれはバッファーで保留になります。最後に、Scannerを(ローカルに再宣言する代わりに)メソッドに渡します。同様に、

public static List<Double> getALInfo(Scanner in) { 
    List<Double> inputs = new ArrayList<>(); 
    System.out.println("Please enter 5 - 10 integers, Q to quit: "); 
    while (in.hasNextDouble()) { 
     inputs.add(in.nextDouble()); 
    } 
    in.next(); 
    return inputs; 
} 

次に、あなたのcalculateAvgListをソートし(lowNum値を破棄)subListを取り、最終的に平均値を返す必要がありますgetLowestnum

よう
public static int getLowestnum(Scanner in) { 
    System.out.println("How many of the lowest values should be dropped?"); 
    return in.nextInt(); 
} 

を簡素化することができます。何かのように、

public static double calculateAvg(List<Double> inputs, int lowNum) { 
    Collections.sort(inputs); 
    return inputs.subList(lowNum, inputs.size()).stream() 
      .mapToDouble(Double::doubleValue).average().getAsDouble(); 
} 

そしてmainは、あなたが最初の二重入力された捨てている例

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    List<Double> inputs = getALInfo(in); 
    int lowNum = getLowestnum(in); 
    double average = calculateAvg(inputs, lowNum); 
    System.out.printf("%.2f%n", average); 
}