Java平均值返回无穷大

时间:2015-07-03 11:24:41

标签: java android arrays

我的程序使用Write来保存double(ratingbar.rating)和read以将保存的文件放在数组中。然后,使用数组,我对数组中的数字做了平均值。在读取代码中,当我有getResources(“raw.file”)工作正常并且返回正确时,但现在我需要编写数字,为此我不能使用res文件夹,所以现在我是使用内部存储,我的平均值返回INFINITY。在OnClickListener按钮中调用平均值。 我会发布代码:

private void writeMyArray(double rate){
    //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("arraymedia.txt", true)));
    //.println(rate);
    double ratex2 = rate * 2;
    int erate = (int)ratex2;
        try{
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myarray.txt", Context.MODE_APPEND));
            outputStreamWriter.append(Integer.toString(erate));
            outputStreamWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

private void readMyArray(ArrayList<Double>array){
    String ret = "";
    try {
        InputStream inputStream = openFileInput("myarray.txt");

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();
            int enc = 0;
            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);
                array.add(enc, Double.parseDouble(receiveString));
                ++enc;
            }
            inputStream.close();
        }
    } catch (FileNotFoundException e) {
            e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

平均

private double media(ArrayList<Double>array)
{
    double total = 0;
    double media;
    int a = 1;
    for (int i=0; i<array.size(); i++)
    {
        total = total + array.get(i);
        a=i;
    }
    media = total / a;
    return media;
}

按钮

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            rating = ratingbar.getRating();

           writeMyArray(rating);
            readMyArray(arraydays);
            button.setText(getText(R.string.obrigado) + "!" + media(arraydays));
            //button.setText(getText(R.string.obrigado)+"!");
            ratingbar.setEnabled(false);
            button.setEnabled(false);
        }
    });

3 个答案:

答案 0 :(得分:3)

如果您的分母为0.0(浮点数或双精度数为0),则除以java中的无穷大。分开前请检查。

答案 1 :(得分:0)

在你的循环中有一个错误,不要使用a=i,导致(如果只有一个元素)a=0的结果为无穷大。使用i+1代替在每次迭代中添加1

int i = 0;
for (i=0; i<array.size(); i++)
{
    total = total + array.get(i);
    // a=i;  // wrong
}
media = total / (i + 1);
return media;

答案 2 :(得分:0)

private double media(ArrayList<Double>array){
    double total = 0;
    double media;
    int a = array.size(); // set to size of array, instead of setting to i
    for (int i=0; i<array.size(); i++) {
        total = total + array.get(i);
    }
    if(a==0) //if a == 0 // divide by 0 -> infinity
        media = 0;
    else
        media = total / a;
    return media;
}