0

好吧,我在我的类 Object2D 中使用了这个方法 Resize,它应该调整 Object2D 的二维颜色数组 PointInformation 的大小,它被调用到某个百分比。(我发现将 2D 阵列转换为 1D 阵列时更容易做到)

public class Object2D

{
int width;
int height;
int ResizePercentage = 100;
Color PointInformation[][];


public void Resize(int Percentage)
{
    Color[]temp = Standart_Methods.Reduce2DArray(this.PointInformation);
    int temp_width = this.width;
    int temp_height = this.height;
    double Faktor = (Percentage+100)/100;
    this.width = (int) (this.width*Faktor);
    this.height = (int) (this.height*Faktor);
    this.ResetPointInformation();
    Color[]temp2 = Standart_Methods.Reduce2DArray(this.PointInformation);

    int SamePixelCount = 0;
    Color LastColor = temp[0];
    for (int i = 0; i < temp.length; i++)
    {
        if (temp[i] == LastColor )
        {
            SamePixelCount += 1;
        }
        else
        {
            for (int i2 = (int) (i*Faktor); i == 1; i-- )
            //Method Resize will only be called when i*Faktor is going to be 100% = X.0 (An Integer)
            {
            temp2[i*2-i] = LastColor;      
            }
            SamePixelCount = 0;
        }
    }
    Standart_Methods.PrintArray(temp2);
    int a = 10;
    int b = 0;
    System.out.print(a/b); //No Exeption, Code unreachable!?       
}
}

它基本上从 temp[0] 开始,只要找到相同的颜色,就将 int SamePixelCount 加 1。当找到不同的颜色时,该方法将先前像素的颜色写入 temp2 数组中的正确位置。

for (int i = 0; i < temp.length; i++)
{
    if (temp[i] == LastColor )
    {
        SamePixelCount += 1;
    }
    else
    {
        for (int i2 = (int) (i*Faktor); i == 1; i-- )
        //Method Resize will only be called when i*Faktor is going to be 100% = X.0 (An Integer)
        {
        temp2[i*2-i] = LastColor;      
        }
        SamePixelCount = 0;
    }
}

操作数组 temp2 到对象的 PointInformation 的正确转换仍然缺失,因为我想测试 temp2 是否正确调整了 temp 的大小,所以我做了

Standart_Methods.PrintArray(temp2); //the Method works btw

但它什么也没做!甚至更糟!我放在那个命令的地方的所有东西,也没有!

    int a = 10;
    int b = 0;
    System.out.print(a/b); //No Exeption! 

更奇怪的是,一旦我调用 Method Resize,在某个地方,调用之后的所有内容都变成了同样奇怪的无法访问的代码!?

我对可能导致此问题的原因完全一无所知。

你能帮忙的话,我会很高兴!

4

1 回答 1

0

除以零肯定会给你 ArithmeticException:

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 0;
        System.out.println(a/b);
    }
}   

Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:14)

我建议使用 eclipse 并使用调试器跟踪您的代码。检查每个代码行的变量,我相信您可以找出我们的问题所在

于 2012-11-28T23:56:21.387 回答