对于家庭作业,我必须构建一个 10x10“位图”,它将打印房屋的简单图像,作为其自己的类构建。构造函数需要一个 int[]
填充每个点的坐标,这样 1 , 1 , 1 , 2
值相当于坐标点 (1,1) 和 (1,2)
但是当我尝试打印数组时,我对发生的情况感到困惑。我的一些点位于错误的位置,一些缺失,但大多数都位于正确的位置。填充和打印后,位图应该看起来像
--------- ----------
-o-oo---- -o-o------
-oo--o--- -oo-o-----
-o----o-- -o---o----
oo----oo- oo----o---
-o-o--o-- but my result is -o-o---o--
-o-o--o-- -o-o--o---
-o-o--o-- -o-o--o---
-oooooo-- -oooooo---
--------- ----------
以下是我的构造函数...
String image[][] = new String[10][10];
Bitmap(int a[]) {
//CONSTRUCTOR
for (int i = 0; i < 10; i++) { //set all to '-'
for (int j = 0; j < 10; j++) {
image[i][j] = "-";
}
}
int c = 0; // because it takes too long to type out a proper for loop
for (int i : a) { //read data and plot points
if(c % 2 == 1) temp2 = a[c];
else temp1 = a[c];
if(c % 2 == 0 && c != 0)
image[temp1][temp2] = "o";
c++;
}
}
这是我的观点。
int points[] = { 1, 1, 1, 3, 1, 4, 2, 1, 2, 2, 2, 5, 3, 1, 3, 6, 4, 0, 4, 1, 4, 6, 4, 7, 5,
1, 5, 3, 5, 6, 6, 1, 6, 3, 6, 6, 7, 1, 7, 3, 7, 6, 8, 1, 8, 2, 8, 3, 8, 4,
8, 5, 8, 6
翻译为:
(1,1) (1,3) (1,4) (2,1) (2,2) (2,5) (3,1) (3,6) (4,0) (4,1) (4,6) (4,7) (5,1) (5,3) (5,6)
(6,1) (6,3) (6,6) (7,1) (7,3) (7,6) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6)
另一方面,我的结果打印为
(1,1) (1,3) ( , ) (2,1) (2,2) (2,4) (3,1) (3,5) (4,0) (4,1) (4,6) ( , ) (5,1) (5,3) (5,7)
(6,1) (6,3) (6,6) (7,1) (7,3) (7,6) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6)
我不知道自己做错了什么,也不知道从哪里开始。如果我能得到一些指示或者对我的代码中的缺陷的评论,那就太好了。
最佳答案
First of all I suggest naming your variables maybe
int x
andint y
. And I do wonder why you are setting the Bitmap-point too
whenc % 2 == 0
should'nt you test for if(c % 2 != 0)
? –Schokokuchen Bäcker
这条评论来自Schokokuchen Bäcker解决了问题。
在上面的循环中,行 if(c % 2 == 0 && c != 0)
开始在第三迭代而不是第二迭代上设置[temp1][temp2] = "o"
,这会导致在代码的一开始就排除一个点。
关于java - 二维位图在不正确的位置填充了对象 [作业],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43457730/