3

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;
                }
            }
        }

    }
}   
4
  • 1
    I don't see a question here or a problem (besides a homework problem). Run it in a debugger/IDE and watch all the variables and output window to ascertain what is happening. Or, just step through it on paper (or in your head, it's not too complex). Commented Oct 14, 2015 at 20:15
  • Well the problem is that on the return leg \r does not clear every 9. I am unable to find what is causing this also I write in eclipse and can not find the error. (the code doesn't function as intended in eclipse anyway as for some reason it jumps a line but in terminal all output happens on one line) Commented Oct 14, 2015 at 20:31
  • 1
    Couple of unrelated comments on your code: You have 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 be if (dir == true) { ... } else /*dir must be false*/ { ... } Also, don't test if some_boolean == true just test some_boolean: if (dir) { ... } else { ... } That's confusing, since "dir" (direction?) doesn't tell you much: what does dir == true` mean? If it means "moving right" and false means "moving left" rename dir to movingRight ... if (movingRight) { ... } else { /* moving left */ } Commented Oct 14, 2015 at 22:32
  • I too will go by Stephen suggestion. @heathbm. Clearly you need to improve your programming logic. And when programming a better naming always helps. Though I likes the usage of \r or carriage return Commented Oct 14, 2015 at 23:07

1 Answer 1

3

Explaining why you are getting such result

Basically in your loop you are changing where to place carriage return \r.

After loop completion when you come back to i = 1. your are changing after how many characters you are going to do a carriage return.

1 -> _9\r
2 -> __9\r
3 -> ___9\r
4 -> ____9      \r
x3 -> ___9       \r
x2 -> __9        \r
x1 -> _9\r

thus the last statement does not clear the 9 at x2

Change needed in if (dir == true)

System.out.print(" \r" ); // enter a space before \r

or it would be best to use

System.out.print("               \r" ); // preferable

you can also remove your sysout \r from the condition loop and use it before your Thread sleep

System.out.print("       \r" );
Thread.sleep(1000);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the feedback, I now use your second method and now I understand where I initially went wrong. much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.