This question already has an answer here:
- How do I compare strings in Java? 23 answers
I have an if statement here and there are no errors but it gives the wrong result:
Initial Code
import java.util.Scanner;
public class Decision {
public static void main(String args[]){
String enter;
Scanner inputanswer = new Scanner(System.in);
System.out.println("welcome to a test");
System.out.println("proceed? yes or no?");
String decision1 = inputanswer.nextLine();
if ( decision1 == "yes" ){
System.out.println("lets proceed");
} else if ( decision1 == "no" ){
System.out.println("Quitting now");
} else {
System.out.println("invalid");
}
}
}
so when I type in yes or no for example it still comes out as invalid.
Also can someone help me put this in a loop so that when they do an invalid statement it resets to the question.
Aucun commentaire:
Enregistrer un commentaire