【问题标题】:Keeping track of previous numbers in a for loop is not working在 for 循环中跟踪以前的数字不起作用
【发布时间】:2014-07-25 18:22:44
【问题描述】:

我知道网上还有这样的问题,但我的代码无法正常工作。我不知道我的 if 语句是否不正确或是否有其他问题。这是我之前问题的延伸 Remove duplicates in a 2D array and add the corresponding values

问题仍然相同,但除了使用 for 循环而不是使用 2D 数组之外,我不想跟踪 for 循环中以前的数字,但是当我将数字打印到控制台时,我获得重复的 r 值。我希望有人可以帮助我看到错误。提前致谢!

这是我正在处理的代码:

    double rMax = -1;
        double previous;
         for(int i =1; i< 256; i++){
                for(int y =1; y< 256; y++){
                    //image.getPixel(i, y);
                    String x = image.getLocationAsString(i, y);
                    String n = image.getValueAsString(i, y);


                    String delim = ", value=";
                    String [] tokens = n.split(delim);
                    double num = Double.parseDouble(tokens[1]);

                    //if(image.getR() < 1.43){
                        String [] t = x.split("r=");
                        String[] b = t[1].split(" mm/c");
                        //System.out.print("Meet b:    "+b[0]);
                        double radius = Double.parseDouble(b[0]);

                        String [] theta = x.split("theta= ");
                        String [] token2 = theta[1].split(Character.toString(IJ.degreeSymbol));
                        float thetaNum = Float.parseFloat(token2[0]);
                        //System.out.print("  This is the theta value:    "+thetaNum+"    ");

                        if(radius == rMax){
                            rMax = radius;
                        }

                        String prevX = image.getLocationAsString(i-1, y-1);
                        String prevN = image.getValueAsString(i-1, y-1);

                        String [] prevT = prevX.split("r=");
                        String[] prevB = prevT[1].split(" mm/c");
                        //System.out.print("Meet b:    "+b[0]);
                        double prev_radius = Double.parseDouble(prevB[0]);
                        previous = prev_radius;

                        if(previous == radius){
                            System.out.println(radius);
                        }
                        else{
                            System.out.println(radius);
                        }

                        //if(thetaNum <= 180.00){
                        data.add(radius, num);

                        //}
                    //}
                }
            }
         System.out.println(rMax);

这是打印出来的:

1.59
1.59
1.58
1.58
1.57
1.56
1.56
1.55
1.55
1.54
1.54
1.53
1.52
1.52

【问题讨论】:

    标签: java loops duplicates


    【解决方案1】:

    您没有在任何地方设置previous 的值。

    更新

    您的代码有很多问题。例如,它永远不会更新 rMax。我认为这段代码将完成您想要实现的目标,假设 data 是某种字典。如果不是,您需要修改代码以更新为半径存储的值。

    double rMax = -1;
    double previous = -1;
    for(int i =1; i< 256; i++){
        for(int y =1; y< 256; y++){
            String x = image.getLocationAsString(i, y);
            String n = image.getValueAsString(i, y);
    
            String delim = ", value=";
            String [] tokens = n.split(delim);
            double num = Double.parseDouble(tokens[1]);
    
            String [] t = x.split("r=");
            String [] b = t[1].split(" mm/c");
            double radius = Double.parseDouble(b[0]);
    
            String [] theta = x.split("theta= ");
            String [] token2 = theta[1].split(Character.toString(IJ.degreeSymbol));
            float thetaNum = Float.parseFloat(token2[0]);
    
            if(radius > rMax){
                rMax = radius;
            }
    
            if(radius == previous){
                data[radius] += num;
            }
            else {
                data.Add(radius, num);
            }
         }
    }
    System.out.println(rMax);
    

    顺便说一句,如果您使用正则表达式从字符串中捕获值而不是所有令人困惑的拆分,则此代码将因此更易于阅读。

    【讨论】:

    • 对不起,我在复制和粘贴时忘记在问题中添加它,但它似乎仍然无法修复它。我不知道它的 if 语句是否不正确。因为我只希望重复的 r 值打印一次而不是两次。所以我不知道代码有什么问题。
    • 我在我的问题中添加了最近更新的代码。但我似乎无法摆脱重复。
    • 好的,现在无论radius == previous 与否,你的代码都会做同样的事情。 if 和 else 块是相同的,并且无论如何都会将值添加到 data 集合中。
    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 2018-11-25
    • 2015-07-11
    • 2015-12-18
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多