如果条件被跳过以根据某些条件计算循环中的平均值

时间:2015-11-09 16:18:49

标签: java

我想取平均值,而A列中的值低于其下一个值,B列中的值与下一个值相同。平均值取自基于列A值的列C值作为列C索引。以下是数据样本:

columnA,B,C
0,0,0.36
1,0,0.23
2,0,0.14
3,1,0.41
4,1,0.44
5,2,0.16
6,2,0.03
7,2,0.09

以下是我目前的代码:

   import java.io.*;
   import java.util.*;

   public class dtw_post {


    public static void main(String[] args) throws Exception {
        //Scanner scan = new Scanner(System.in);
        int N = 655;
        int x = 655;
        File file = new File("box_raw.txt");
        Scanner scannerFile = new Scanner(file);

         while(scannerFile.hasNextLine()){
                String line = scannerFile.nextLine();
                String[] lineVector = line.split(",");

        int a[] = new int[N];
        int b[] = new int[N];
        double c[] = new double[x];


        for(int i = 0; i<N; i++)
        {
        a[i]=Integer.parseInt(lineVector[0]);
        b[i]=Integer.parseInt(lineVector[1]);   
        c[i]=Double.parseDouble(lineVector[2]);
        }

        System.out.println((dtw_post.lookup(a,b,c)));}
        }

    static String lookup (int[] a, int[] b, double[] c){
        int j=0; int i=0;
        String[] final_result = new String[c.length];
        while(i < a.length-1){

        if (a[i] < a[i+1] && b[i] == b[i+1]) {
            double[] d = {c[a[i]],c[a[i+1]]};
            double sum = 0;
            int number = 0;
            for (double val : d) {
                ++number;
                sum += val;
            }
            double e = sum/number;
            final_result[i] = String.valueOf(e); 
            i++; 

        }
        else {
            final_result[i] = String.valueOf(c[a[i]]);
            i++;
        }
        }
        String result = final_result[j]; j++;
        return result;
    }
}

我预计它在第一行输出0.243,平均值为0.36,0.23和0.14。我认为问题出在查找方法的if条件中。它似乎跳过if并仅运行else条件。我当前的代码输出列C完全如此。我的if条件循环出了什么问题?谢谢你的时间。

3 个答案:

答案 0 :(得分:1)

第一个问题是您正在解析文件错误。您正在while循环中逐行解析文件。但您要为每一行创建新的abc数组。将初始化移出for循环:

public static void main(String[] args) throws Exception {
    //Scanner scan = new Scanner(System.in);
    int N = 655;
    int x = 655;

    int a[] = new int[N];
    int b[] = new int[N];
    double c[] = new double[x];
    int i = 0;

    File file = new File("box_raw.txt");
    Scanner scannerFile = new Scanner(file);

    while (scannerFile.hasNextLine()) {
        String line = scannerFile.nextLine();
        String[] lineVector = line.split(",");

        a[i] = Integer.parseInt(lineVector[0]);
        b[i] = Integer.parseInt(lineVector[1]);
        c[i] = Double.parseDouble(lineVector[2]);
        ++i;
    }

    System.out.println((dtw_post.lookup(a, b, c)));
}

基于此,您将只获得两个连续值的平均值。您还需要重新考虑查找功能。增加总和,直到b中的值发生变化,然后计算平均值并打印出来。 E.g

static String lookup(int[] a, int[] b, double[] c) {

    String result = "";

    double sum = c[a[0]];
    int consecutive = 1;

    for (int i = 1; i < a.length - 1; ++i) {
        if (a[i - 1] < a[i] && b[i - 1] == b[i]) {
            sum += c[a[i]];
            ++consecutive;
        } else {
            result += String.valueOf(sum / consecutive) + '\n';
            sum = c[a[i]];
            consecutive = 1;
        }
    }

    return result;
}

请注意,您还应该考虑StringBuilder在循环中连接字符串

答案 1 :(得分:0)

在你的if条件中你要求a [i]小于[i + 1],不应该是[i]大于[i + 1]吗?

所以if应该是:

if(a[i]>a[i+1] && b[i]==b[i+1]){
    ....

答案 2 :(得分:0)

您一次只处理文件的一行。循环从文件中读取一行并将其拆分以获取a,b,c的值。然后,它将这些值复制到相应数组中的每个单元格中,然后调用查找。由于a的每个值都相同,因此if语句永远不会成立。

您要做的是在a,b,c中填写下一行读取的每个连续单元格,并在while循环外部调用查找。我认为你想要的是:

public static void main(String[] args) throws Exception {
    //Scanner scan = new Scanner(System.in);
    int N = 655;
    File file = new File("box_raw.txt");
    Scanner scannerFile = new Scanner(file);

    int a[] = new int[N];
    int b[] = new int[N];
    double c[] = new double[N];

    int i = 0;
    while(scannerFile.hasNextLine()){
        String line = scannerFile.nextLine();
        String[] lineVector = line.split(",");

        a[i]=Integer.parseInt(lineVector[0]);
        b[i]=Integer.parseInt(lineVector[1]);
        c[i]=Double.parseDouble(lineVector[2]);
        i++;
    }

    System.out.println((dtw_post.lookup(a,b,c)));
}