This question already has an answer here:
- How do I compare strings in Java? 23 answers
This is a joke program that i just wrote for this question but i do have some serious programs that i have tried this with. Im unsure of how to get this to work. If i have multiple while loops or if else statements that are nested within each other, then how does java know which closing curly brace { applies to which loop? For instance if i type the following code: package fiahsticks;
import java.util.Scanner; public class fishsticks {
public static void main(String[] args) { Scanner scan = new Scanner (System.in);
String fishSticks = "";
String keepGoing = "yes";
String go = "go";
while (keepGoing == "yes"){
System.out.print ("do you like fish sticks");
fishSticks = scan.nextLine();
if (fishSticks == "yes"){
System.out.println ("your a gay fish");
keepGoing = "null";
}
else if (fishSticks == "no"){
System.out.println ("wrong answer");
keepGoing = "yes";
}
}
}
}
Java just continues to prompt me with the do you like fish sticks question and never makes it to the if statements. I also have another program that is bigger which contains multiple if else statements that are all nested within a while loop but this program runs into a different issue. Java goes through the while loop and makes it to the if statement but when it hits the closing } of the if statement, java thinks that this is the closing curly brace for the while loop and the program quits. Why is this? If this were python then I could tell the program which statements are nested in what simply by indenting them, but java doesn't seem to work this way. How does java know what is nested inside of what?
Aucun commentaire:
Enregistrer un commentaire