简体   繁体   中英

x cannot be resolved to a variable

I've ran into a few problems with my code. I'll highlight the errors in bold (asterisks **) :) Thanks in advance (code highlighted has this error: x cannot be resolved to a variable")

package com.cmnatic.reborn.gui;

public class Bitmap {
    public final int width;
    public final int height;
    public final int[] pixels;

    public Bitmap(int width, int height) {
        this.width = width;
        this.height = height;
        pixels = new int[width * height];
    }

    public void draw(Bitmap bitmap, int xOffs, int yOffs)  {
        for (int y = 0; y<bitmap.height; y++) {
            int yPix = y+yOffs;
            if (yPix<0 || yPix>=height) continue;

            for(int xPix = 0; **x**<bitmap.width; x++) {
                int **xPix** = **x+x0ffs**;
                if (xPix<0 || xPix>=width) continue;

                pixels[xPix+yPix*width] = bitmap.pixels[x + y * bitmap.width];
            }
        }
    }
}

Also, I've got an odd problem which I'm not sure about: (code below highlighted has the following error: "Duplicate local variable xPix"

for(int xPix = 0; **x**<bitmap.width; x++) {
    int **xPix** = **x+x0ffs**;
    if (xPix<0 || xPix>=width) continue;

Thanks in advance, CMNatic

xPix is already defined within the scope of the inner for loop. x needs to be declared before it can be used so it makes sense to use

for (int x = 0; x < bitmap.width; x++) {

Also

int xPix = x + x0ffs; // typo

should be

int xPix = x + xOffs;

As always the error messages provide a meaningful indicator as to the nature of the issues...

x is no where declared but being used in the loop, So the error. Correct your loop variable to x

for(int x = 0; x<bitmap.width; x++) {
        int xPix = x+x0ffs;
        if (xPix<0 || xPix>=width) continue;

All the problems will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM