1
int time=0;
int x=1;
int w=2;
int y=1;
int h=1;
int t=6;
while (x != w && y != h) {
    boolean s = Abc (a,b,c,time);
    if (s == true) {
        time = time+t;
        x++;
    }
    else if (s = false) {
        time = time++;
    }
}

System.out.println(time);

This is a part of program I'm writing for school and my problem is that when the program enters the while loop no matter what I do inside the loop when it exits and prints the variable "time" the value is the same as the value set at the beginning. Am I missing something? (BTW Abc is just a method that should return the value true when I first enter the loop, therefore the value of time should be changed to time+t)

5
  • Try with else if (s == false) (even if it is a little too overkill) Commented Nov 9, 2015 at 13:19
  • (s = false) should be: == false Commented Nov 9, 2015 at 13:19
  • s = false -> s == false else it will set s to false instead of comparing it, also time = time++ does nothing. Use time++ instead Commented Nov 9, 2015 at 13:20
  • 5
    The code you've posted will never enter the while loop, since y and h are equal. Commented Nov 9, 2015 at 13:22
  • 4
    You never need to compare a boolean; if (s) { ... } else { ... }; Commented Nov 9, 2015 at 13:22

1 Answer 1

7
  1. s = false is an assignment, not a comparison. Anyway, you don't need to test if s == false, you just need an else clause (since the original condition checks for s == true).
  2. There's no reason to assign the result of time++ to time, since it will undo the increment (the post increment operator returns the previous value of the variable, so when you assign that value back to the variable, you cancel the increment).

Change

else if (s = false) {
    time = time++;
}

to

else {
    time++;
}

Edit :

As Anthony Grist commented, even after these fixes, the entire while loop won't be executed at all, since y==h in the beginning.

Sign up to request clarification or add additional context in comments.

2 Comments

Might be worth tipping a nod to @AnthonyGrist's comment, since these fixes don't make time change in and of themselves (UV anyway).
@AndyTurner I missed that. Thanks

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.