[英]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];
}
}
}
}
另外,我还有一个我不确定的奇怪问题:(下面突出显示的代码具有以下错误:“重复的局部变量xPix”
for(int xPix = 0; **x**<bitmap.width; x++) {
int **xPix** = **x+x0ffs**;
if (xPix<0 || xPix>=width) continue;
预先感谢,CMNatic
xPix
已在内部for
循环的范围内定义。 必须先声明x
才能使用它,因此使用有意义
for (int x = 0; x < bitmap.width; x++) {
也
int xPix = x + x0ffs; // typo
应该
int xPix = x + xOffs;
与往常一样,错误消息提供了有关问题性质的有意义的指示...
x
在声明的地方没有,但是在循环中使用,所以是错误的。 将循环变量更正为x
for(int x = 0; x<bitmap.width; x++) {
int xPix = x+x0ffs;
if (xPix<0 || xPix>=width) continue;
所有的问题都将得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.