Correct. But you don't need the different instructions in the loop. You can simplify your code when you realise that lines 4 and 7 test exactly the same thing.need a loop to iterate until a condition is met . . . execute a different set of instructions.
Campbell Ritchie wrote:Alternative, which is not semantically equivalent to Carey's examples:-I think I would use var instead of int. Remember var can only be applied to local variables.
You and I have interpreted the question differently, and as I said earlier, our two solutions are definitely not equivalent to each other. I shall leave it as an exercise for the reader to work out whether they can be made equivalent by swapping my lines 5 and 6. [They probably can't.]Carey Brown wrote:. . . your logic which I don't believe Campbell's quite matches. . . . .
It is helpful to know the full details; it does no good to answer a question different frrom what you actually want answered. I did give you some hints last night.w curawn wrote:. . . I cannot guarantee that current always increments (I failed to say that in my original post, sorry) . . .
Not in that code it isn't. Do you mean that you are actually using somethingElse.getSb() in that loop? That is wasteful of resources, and it is probably not a good idea to use a loop at all. Least of all in Swing because you are either using multiple threads on a thread‑unsafe object, or occupying the whole of the EDT with a loop running 10⁸× a second for something occurring every few seconds. That can make your GUI unresponsive.. . . sb is from an external source. . . .
How many times should it run and how many times does it run? But I now think a loop isn't going to work. Consider a timer, as I said last night. Consider some sort of Listener that calls a method whenever somethingElse.getSb() changes.. . . the while loop always continues one to many times.
w curawn wrote:Unfortuntaely what still happens with this is the while loop always continues one to many times.
Please explain some more.w curawn wrote:. . . the loop seems to iterate 1 too many times. . . .
OP is getting msg "again" and "end" printed back to back. What he wants is that when the end condition is met to ONLY print the "end" msg and not the "again" msg.w curawn wrote:I will get the output:
610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click again
618 ==> Click end
What I require is this output:
610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click end
I tried that: didn't work.Junilu Lacar wrote:. . . move line 11 (the System.out.println statement) above line 9.
Add 2 to current before the loop starts. But why do you need the loop at all, let alone running four times?Campbell's JShell wrote:jshell> int current = 608;
...> int target = current + 10;
current ==> 608
target ==> 618
jshell> while (current < target) { <br /> ...> System.out.println(current + " ==> Click again");
...> current += 2;
...> } System.out.println(current + " ==> Click end");
608 ==> Click again
610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click end
w curawn wrote:Your assumption is mostly right. In fact it isn't line 3 //... where the code is, it is the
This is inputted from another program that I have no control over. All I do is check this variable and if it has incremented by 2 then I make my current increment by 2 as well.
OP is getting msg "again" and "end" printed back to back. What he wants is that when the end condition is met to ONLY print the "end" msg and not the "again" msg.
I gave you a solution twice and you haven't said why it won't work for you.
You should by now now whether it will compile and run. If you insist on putting statements inside loop headers, you should use the ?: operator.w curawn wrote:. . . Whether or not it is acceptable is another matter. . . . Could this even work?
If you insist on putting statements inside loop headers, you should use the ?: operator.
If you insist on putting statements inside loop headers…
w curawn wrote:I will get the output:
610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click again
618 ==> Click end
What I require is this output:
610 ==> Click again
612 ==> Click again
614 ==> Click again
616 ==> Click again
618 ==> Click end
Campbell Ritchie wrote:Damn! Such a good bit of code. How did the rest of us miss it?
Campbell Ritchie wrote:
I tried that: didn't work.Junilu Lacar wrote:. . . move line 11 (the System.out.println statement) above line 9.
...
Add 2 to current before the loop starts.
w curawn wrote:this is the best I can come up with
People are giving various suggestions how to approach this problem, but, given a nonsensical (sorry for this word, no intention to be harsh) provided example what need to solve, it is quite difficult to understand whether given suggestions may satisfy your need.
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|