0
Having trouble with my if statements using user input for the variables
I keep getting the error result printed no matter what I type in as the user input. Was hoping to get some help! Thanks in advance! https://code.sololearn.com/cayPkXmF29Bu/?ref=app
6 Answers
+ 2
Because of ==
If you compare int, long, char or other primitive data types then == is okay.
But for String you need equals().
If(choice.equals("attack"){
//print attack
}else if(choice.equals("defend"){
//print defend
}else{
//print error
}
+ 1
About comparing objects:
https://www.sololearn.com/learn/Java/2178/?ref=app
+ 1
thank so much !!
0
What did you type as user input? I typed 1 and 2, and it's doing alright for both run
0
Oh I had just changed it to use int as the values. I orginally was using a string variable and getting user input through âoption.next();â
0
sorry about that ive changed it back to what i had originally