【问题标题】:Variables in java not holding their value inside loopjava中的变量没有在循环中保存它们的值
【发布时间】:2013-01-12 01:15:44
【问题描述】:

我正在遍历圆 (r) 的可能半径,当我找到潜在的候选者时,我基本上想将它们推入堆栈。我只需要记住最后两个半径和它们对应的圆的颜色,所以我创建了四个整数来存储这些变量。但是,我包含的打印语句总是为两个变量生成 0 值。

 //small stack to hold values of previous circles
            int small_radius    =   0;
            int small_color     =   0;
            int med_radius      =   0;
            int med_color       =   0; 
            //iterate through possible radii       
            while (r<max){
                //check for possibility of a circle
                if(detectCircle(x,y,r,img,g)){
                    //confirm it is a circle and store its color
                    int large_color = confirmCircle(x,y,r,img,g);
                    if(large_color != -1){
                        //if it is a circle, compare the medium circle against the current one and the small one
                        //check if the current circle and small circle do not immediately surround the medium circle
                        boolean matches_small = (med_radius-1 == small_radius && med_color == small_color);
                        boolean matches_large = (r-1 == med_radius && large_color == med_color);
                        if(!matches_small && !matches_large){
                            //if it is a circle of single line thickness, draw it
                            System.out.println("med_radius: "+med_radius+" small_radius: "+small_radius);
                            drawCircle(x,y,r,img,g);
                        }
                        //now push the current circle onto the stack.
                        small_radius = med_radius;
                        small_color  = med_color;
                        med_radius   = r;
                        med_color    = large_color;  
                    }
                }
                r++;
            } 

编辑:对于那些想知道 confirmCircle 是什么样子的人,这里是,但它不应该有所作为。

 static int confirmCircle(int cx, int cy, int r, BufferedImage img, Graphics2D g) {

    int color = img.getRGB(cx,cy+r);

    int f = 1-r;
    int ddF_x = 1;
    int ddF_y = -2 * r;
    int x = 0;
    int y = r;


    while(x < y) {
        if(f >= 0) {
            y--;
            ddF_y += 2;
            f += ddF_y;
        }
        x++;
        ddF_x += 2;
        f += ddF_x;

        if(img.getRGB(cx+x,cy+y) != color){color = -1;}
        if(img.getRGB(cx-x,cy+y) != color){color = -1;}
        if(img.getRGB(cx+x,cy-y) != color){color = -1;}
        if(img.getRGB(cx-x,cy-y) != color){color = -1;}
        if(img.getRGB(cx+y,cy+x) != color){color = -1;}
        if(img.getRGB(cx-y,cy+x) != color){color = -1;}
        if(img.getRGB(cx+y,cy-x) != color){color = -1;}
        if(img.getRGB(cx-y,cy-x) != color){color = -1;}
    }
    return color;
}

【问题讨论】:

  • 调试器指向什么?你试过用吗?
  • 没有confirmCircle的代码,就无法知道问题出在哪里。如果它每次都返回 -1,你会得到你看到的结果。我们需要更多信息才能提供重要帮助。
  • 您的 println 语句在分配 med_radius 之前。它应该使用 r 来显示 med_radius 吗?由于 med_radius 等于 r。
  • @JonathanDrapeau 如果返回 -1,则不调用打印语句。
  • 奇怪.. 我刚刚测试了你的代码(最大值 = 10,伪造方法只返回将进入循环的值,并强制布尔值为假,因此它保证打印)并且打印了所有内容像它应该的那样递增......

标签: java variables loops scope


【解决方案1】:

此示例中没有代码分配给四个变量中的任何一个。

如果您在 confirmCircle() 内进行赋值,那么问题是您期望 ints 在 Java 中按值传递时是按引用传递。

换句话说:

int a = 4;

public void changeA(int someVar) {
    someVar++;
}

changeA(a);
System.out.println(a);  //prints 4, not 5.

【讨论】:

  • confirmCircle() 返回圆的 RGB 值,然后我将其分配给此循环内的变量。
猜你喜欢
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 2022-11-17
  • 2013-10-29
相关资源
最近更新 更多