用绿色交换红色,反之亦然

时间:2015-12-05 23:03:36

标签: javascript

我已经在这个问题上工作了一段时间,但我无法弄清楚我需要修复我的代码。我应该可以将颜色红色换成绿色,但在我的例子中我一直收到错误(我可以将红色设置为绿色,但反之亦然。有人可以看看我出错的地方吗?

class uptrsq_range(object):

    def __init__(self, n):

        self._n_ = n
        self._length = n*(n+1) // 2

    def __iter__(self):
        for ii in range(self._n_):
            for jj in range(ii+1):
                yield (ii,jj)

    def __len__(self):
        """
        recepe by sleblanc @ stackoverflow
        """
        "This method returns the total number of elements"
        if self._length:
            return self._length
        else:
            raise NotImplementedError("Infinite sequence has no length")
            # or simply return None / 0 depending
            # on implementation

for i,j in uptrsq_range(len(x)):
    score(x[i], x[j])

5 个答案:

答案 0 :(得分:2)

您需要使用临时变量,因为在您尝试设置绿色时红色已经更改。

var oldRed = pixel.getRed();

pixel.setRed(pixel.getGreen());
pixel.setGreen(oldRed);

答案 1 :(得分:0)

您需要添加一个临时变量以交换红色和绿色的值

 private void startScanning(final boolean enable, BluetoothAdapter mBluetoothAdapter) {
    bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    List<ScanFilter> scanFilters = new ArrayList<>();
    final ScanSettings settings = new ScanSettings.Builder().build();
    ScanFilter scanFilter = new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(UUID_SERVICE)).build();
    scanFilters.add(scanFilter);
    bluetoothLeScanner.startScan(scanFilters, settings, scanCallback);

}

答案 2 :(得分:0)

这是我想出的解决此问题的代码。方法是创建两个变量,一个变量用红色初始化,另一个变量用绿色初始化,然后稍后将它们交换。

    var MyImage = new SimpleImage("eastereggs.jpg");
    print(MyImage);

    function swapRedGreen(pixel) {  
        var newGreen = pixel.getRed();  
        var newRed = pixel.getGreen();  
        pixel.setGreen(newGreen);   
        pixel.setRed(newRed);   
        return pixel;
    }

    for (var pix of MyImage.values()){
        swapRedGreen(pix);
    }
    print(MyImage);

答案 3 :(得分:-1)

       var image = new SimpleImage("usain.jpg");
function swapRedGreen(image){
    for (var pixel of image.values()){
    pixel. setRed(pixel.getGreen());
    pixel. setGreen(pixel. getRed());

    }
}
  swapRedGreen(image);
    print(image);

答案 4 :(得分:-1)

var img = new SimpleImage("eastereggs.jpg");

function swapRedGreen (pixl) {
    var oldRed = pixl.getRed();
    pixl.setRed (pixl.getGreen());
    pixl.setGreen (oldRed);

}
for (var pix of img.values()) {
    swapRedGreen(pix);
}

print (img);