For this code to run as intended use your cmd/terminal not an IDE. (or you will not see the correct effect of \r)
When the 9 in the terminal reaches the right most position after 4 spaces it will turn around and move back 4 spaces and on and on. However when it reaches the start point after a full cycle there is a remaining 9 on the second space that is not cleared by \r. What could be causing this?
public class Core {
public static void main(String[] args) throws InterruptedException {
int Array[][] = new int[4][6];
int score;
boolean enemy = true;
boolean dir = true;
// EnemyLine
while (enemy) {
for (int i = 1;;) {
Thread.sleep(1000);
if (i == 1)
dir = true;
else if (i == 4)
dir = false;
if (dir == true) {
int a = Array[0][i] = 9;
System.out.printf("%" + i + "d", a);
i++;
System.out.print("\r");
continue;
} else if (dir == false) {
int a = Array[0][i] = 9;
System.out.printf("%" + i + "d", a);
i--;
System.out.print(" \r");
continue;
}
}
}
}
}
if (dir == true) { ... } else if (dir == false) { ... }Well since dir is a boolean it can only be true or false so your if..else could beif (dir == true) { ... } else /*dir must be false*/ { ... }Also, don't test ifsome_boolean == truejust testsome_boolean:if (dir) { ... } else { ... } That's confusing, since "dir" (direction?) doesn't tell you much: what doesdir == true` mean? If it means "moving right" and false means "moving left" renamedirtomovingRight...if (movingRight) { ... } else { /* moving left */ }