0

这是我遇到问题的代码。我需要能够阻止数组超过值 255。我坚持应该如何构造这个 if 语句.. 帮助会很棒..

public short[][] Brighten(short[][] original){

    short[][] result1 = new short[original.length][original[0].length];

    for(int i =0;i< result1.length; ++i){
        for(int j = 0;j<result1[0].length;++j){
            result1[i][j] = (short) (20 + original[i][j]);
            //work on this if statement
            if (original[i][j]> 255){
               result1[i][j] = (short)(20 - original[i][j]);
            }

        }
    }   



    return result1;
}
4

2 回答 2

1

我认为应该是这样的

public short[][] Brighten(short[][] original){

    short[][] result1 = new short[original.length][original[0].length];

    for(int row =0;row< result1.length; row++){
        for(int col = 0;col<result1[row].length;col++){
        if (original[col][row] + 20) < 255) {
            result1[col][row] = original[col][row] +20;
        } else {
            result1[col][row] = 255;
        }         

        }
    }   
    return result1;
}

使用此代码,如果变亮后的值 (+20) 小于 255,则会首先检查原始值

希望您能理解我的回答,如果您有任何问题,请随时在评论中提问:)

于 2013-03-24T03:37:07.477 回答
0
for ( int i = 0; i < result1.length; i++ ) {

    for ( int j = 0; j < result1[0].length; j++ ) {

        if ( original[i][j] + 20 < 255 ) {
            result1[i][j] = original[i][j] + 20;
        } else {
            result1[i][j] = 255;
        }
    }
}
于 2013-03-24T03:32:32.853 回答