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.
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 3 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a function that removes all spaces in the given string. This is my code:
fun nonSpaceString(s: String): String {
var result = " "
for (index in 0..s.length-1) {
if (s[index] != ' ') {
result += s[index]
}
}
return result
}
fun main(){
nonSpaceString("abc d e")
}
But the result is holding "a" and then holding "ab" but the end result holds nothing. Does anyone know what I'm doing wrong here?
You have initialized the result with a space character. So the result starts with a space that may confuse you. Apart from that, your function is working fine.
Additionally, you can use the replace function to remove all spaces from a string
"abc d e".replace("\\s+".toRegex(), "")
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 3 years ago.
Improve this question
I am trying to update the for loop variable i inside the loop, why I cannot update it and the i is always 0?
public class HelloWorld{
public static void main(String []args){
for (int i = 0; i < 10 ; ){
i = i ++;
System.out.println(i);
}
}
}
I am expecting print out
0 1 2 3 4 5 6 7 8 9
but it is a infinite loop and print out all 0.
You can, but with i = i ++; you're not incrementing the counter. Use i++ instead - it's an operator that does not return a new value, only increments the primitive.
Keep in mind, that for loop is meant to contain the information about looping. If you manipulate the counter outside of loop, you might loose track of what is going on. This is not recommended, better use while in that place.
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 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 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)
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.