dimanche 2 décembre 2018

Using If Statements to Change a String's Value

Java student here!

I am trying to understand and use if statements more in my code. I have a very simple example that I am trying to grasp. Say for instance, there was a String called answer with the value "yes". Imagine there is also a boolean called yesOrNo. I want to use an if statement to set the value of yesOrNo to true or false depending on the value of answer.

When I try the very simple code below, I get an error that reads "Type mismatch: cannot convert from String to boolean."

boolean yesOrNo = false;
String answer = "yes";
if (answer = "yes") {
yesOrNo = true;
}

What could I be doing better or differently to convert yesOrNo's value?

Please let me know.

Thank you!

-Mark

Aucun commentaire:

Enregistrer un commentaire