对于我正在研究的项目,我希望能够像下面那样“滚动”一个数组:
这里是我到目前为止的代码:
private boolean[][] display = this.parseTo8BitMatrix("Here");
private int scroll = 0;
public void scroll() {
this.scroll++;
}
//sets the clock's display
public void setDisplay(String s) {
this.display = this.parseTo8BitMatrix(s);
}
//determines the current frame of the clock
private boolean[][] currentFrame() {
boolean[][] currentFrame = new boolean[8][32];
int length = this.display[0].length;
if(length == 32) { //do nothing
currentFrame = this.display;
} else if(length <= 24) { //center
for(int i = 0; i < 8; i++) {
for(int j = 0; j < length; j++) {
currentFrame[i][j+((32-length)/2)] = this.display[i][j];
}
}
this.display = currentFrame; //set display to currentFrame so the display doesn't get centered each time
} else { //scroll
for(int i = 0; i < 8; i++) {
for(int j = 0; j < length; j++) {
if(this.scroll+j <= 32) {
currentFrame[i][j] = this.display[i][j+this.scroll];
} else {
//?
}
}
}
}
return currentFrame;
}
我拥有的代码一直有效,直到数组需要“环绕”到另一侧为止。我在哪里弄错了?
我假设您正在寻找一个适用于其他情况的公式。通常,Modulos对于包装非常有用。您正在寻找的基本上是
currentFrame[i][j]= this.display[i][(j+this.scroll)%32];
我有点困惑
if(this.scroll+j <= 32)
不是<32吗?您的矩阵水平有32个位置。