[英]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")
我将以粗体突出显示错误(星号**):)预先感谢(突出显示的代码存在此错误:x无法解析为变量”)
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" 另外,我还有一个我不确定的奇怪问题:(下面突出显示的代码具有以下错误:“重复的局部变量xPix”
for(int xPix = 0; **x**<bitmap.width; x++) {
int **xPix** = **x+x0ffs**;
if (xPix<0 || xPix>=width) continue;
Thanks in advance, CMNatic 预先感谢,CMNatic
xPix
is already defined within the scope of the inner for
loop. xPix
已在内部for
循环的范围内定义。 x
needs to be declared before it can be used so it makes sense to use 必须先声明
x
才能使用它,因此使用有意义
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. x
在声明的地方没有,但是在循环中使用,所以是错误的。 Correct your loop variable to x 将循环变量更正为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. 所有的问题都将得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.