This question already has an answer here:
- How do I compare strings in Java? 23 answers
I'm working on a basic game and want to use if statements to make it a bit more fun, but when a condition was meet (Ex. Choosing something in a menu) the if statement still wouldn't run.
This is for a basic game made in Java and using a basic text-based game engine that I made. I've tried checking if it would break out of the while loop if the condition was met. But it still didn't break out of my while loop.
public class App
{
public static void bossFight() {
int Health = 100;
int bossHealth = 200;
while(Health > 0) {
System.out.println("Health: " + Health);
System.out.println("Boss's Health: " + bossHealth);
Scanner scan2 = new Scanner(System.in);
System.out.println("Attacks: ");
System.out.println("1. Attack with bow \n 2. Slice with Sword \n 3. Magic");
String attackChoice = scan2.next();
if ( attackChoice == "1" )
{
bossHealth--;
}
if(bossHealth < 0) {
System.out.println("Yeah you beat the boss!");
break;
}
I expect it to lower the boss's health but it just skipped the statement and ran the loop again. When I tried to break the if statement with break; it still didn't break.
Aucun commentaire:
Enregistrer un commentaire