[英]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.