Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm writing a simple game that uses a loop while(game = true).
I set game = false in the code during the loop. When I run the game, it stops at that point but does not terminate.
However, if I use break instead at the same spot the program terminates. Why can't I use game = false?
This expression
game = true
assigns true togame and the result of this expression is always true.
What you intended as to test if the previous value of game was true
while(game == true)
or much simpler
while(game)
Usually you give it a better name like
while(running) {
at the end of the while loop use
while (game = true)
*whatever happens when the game = true*
break();
if that doesn't work try something like this
while game = (true);
while (game == true) {
*whatever happens in game loop*
}
make sure somewhere before you start your game loop, you have something telling the code when to make game = false. If you don't the game will run forever without ever changing back to false.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I was wondering if it was possible to check for multiple strings to be true in one while statement. The || operator doesn't work and I can't think of another way to make this possible.
Even though I know the statement bellow isn't possible, I want another statement with the same functionality.
while (!color1.equals("Red")) || (!color2.equals("Green"))
Your requirement is not quite clear to me so I'll give two possible answers.
If at least one condition must be met then you should use || operator (be careful with your parentheses by the way - they should wrap both conditions):
// at least one condition is met
while ((!color1.equals("Red")) || (!color2.equals("Green"))) {
// do something
}
Otherwise, if both conditions must be met, instead of || you need to use && operator.
// both conditions are met
while ((!color1.equals("Red")) && (!color2.equals("Green"))) {
// do something
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I couldn't figure out why my code is printing forever. I tried a block (will show where I had it originally) but I want it to continue after printing the "help". My code goes as follows. I think it's the for loop... but I haven't touched much on it.
boolean yes = true;
while(yes) {
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
} else if (str1.equalsIgnoreCase("Help")) {
for(int g = 0; g < 2; g++) {
System.out.println(" Accepted commands:\n exit\n help\n load [filename]\n students\n search [partial name]\n assignments\n grades\n student [student name]\n assignment [assignment name]\n");
}
//I added the break here and it did print out once but I did not want it to end the program. With the break I did not need the for statement.
} else if (str1.equalsIgnoreCase("Load")) {
}
The value of str1 is never changed inside the loop. Perhaps you are missing some statement that takes new input from the user.
First off
boolean yes = true;
while(yes) {
You are never setting yes to false
Secondly
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
str1 is never set inside the loop, even to "Exit"
yes is set to true and you never changed it in the loop
read str1 inside the loop:
DataInputStream in = new DataInputStream(System.in);
str1=in.readLine();
while(true) {
str1=in.readLine();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm using a while loop and it won't terminate when it should. If it was working correctly, then it would terminate when randno was either == highbound or == lowbound.
Code of the loop:
do {
do {
randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
direction = getDirection(randno,heading);
} while (robot.look(direction)==IRobot.WALL);
System.out.println(randno);
System.out.println(highbound);
System.out.println(lowbound);
System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);
The output is either 3 3 2 ------, or 2 3 2 ------, so the loop should end. The first loop ends properly (I embedded them to try and get it to work...). What is going wrong?
randno!=lowbound | randno!=highbound is always true, since randno can't be equal to both lowbound and highbound (assuming they are not equal).
Therefore the loop never terminates.
If you wish to terminate when randno is different than both bounds, change your condition to :
while (randno==lowbound || randno==highbound)
If you wish to terminate when randno is the same as one of the bounds, change your condition to :
while (randno!=lowbound && randno!=highbound)
EDIT : based on your question, you want the second option.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
this question might be simple for many of you but im stuck in it:/
this if condition works without while around it, but when i put while i get nothing in log
this REresponse is a response coming from server with a particular id that client sent to it plus a T or F as result to make sure the only client recieves it is the one who sent it
boolean recieved = false
String REresponse = (String) sInput.readObject();
while(recieved=false)
{
if (REresponse.equals("('T','"+newRID+"')")){
Log.i(LOGTAG,"you made it");
recieved = true;
}else if (REresponse.equals("('F','"+newRID+"')")){
Log.i(LOGTAG,"you failed");
recieved=true;
}else{
Log.i(LOGTAG,REresponse);
Log.i(LOGTAG,"some thing is wrong");
also i tried to use case condition but it does accpet this
the condition should be
while (received == false){
//...
}
if you write received = false you make an assignment, the boolean operator is ==
recieved = false. This always results in false so you never go into the loop when
You're assigning rather than equality in condition for while. You need to do as in follows.
while(received == false)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there a way to tell the compiler in Java or Android to not remove some statements in code -which are intended to clean up variables after use to prevent any data remnant in ram-??
would creating a dummy method solve this issue??
these statements basically set the variables to their type-based initial values..
Thanks in advance!
The code that you describe is not dead code.
Dead code is code that will never execute.
Here is an example:
private int secretSchmarr;
public boolean blammo()
{
boolean returnValue;
secretSchmarr = calculateSecretValue();
returnValue = useSecretValue(secretSchmarr);
secretSchmarr = 99; // this is not dead code.
return returnValue;
secretSchmarr = 98; // This is dead code because it can never execute.
}
I answer under the odd assumption that you have a good reason to believe that the code is still useful even though it is dead.
Store the value false in some obfuscated form that the compiler can't understand. Then, conditionally branch to that code using your obfuscated value. The compiler will not know it is dead, so it will not be removed.
I'll use a file for my example, but it is probably not the most efficient way. Say your code that the compiler thinks is dead code was in a function called myCode(). Assume that fin is reading from a file that only contains false followed by EOF
if(Boolean.parseBoolean(fin.next()))
myCode();