This question already has an answer here:
- How do I compare strings in Java? 23 answers
this program is a test adventure game based on simple inputs and outputs of java code
public class textGameTest {
public static void main(String[] args) throws Exception{
String usrName = "Combat Fighter", usrAns1=".";
Scanner reader = new Scanner(System.in);
System.out.print("Welcome to adventure battle quest!\npress enter to continue...");
System.in.read();
while(1==1){
System.out.print("Please enter your name: ");
usrName = reader.next();
System.out.print("You entered " + usrName + "\nis this what you want? \t\nY \tor\t N\n\t");
usrAns1 = reader.next();
System.out.print("you entered "+usrAns1+"\n");
if (usrAns1 == "Y"||usrAns1 == "y"){
break;
}
}
System.out.print("Ok, let the adventure begin "+usrName);
}
Its supposed to break the loop if the user inputs"y" or "Y" but whenever the user inputs y pr Y the code ignores it and just continues the loop... any help? this is what happens when it runs:
Welcome to adventure battle quest! press enter to continue... Please enter your name: Test You entered Test is this what you want?
Y or N y you entered y Please enter your name: Test2 You entered Test2 is this what you want?
Y or N Y you entered Y Please enter your name: Itsnotworking You entered Itsnotworking is this what you want?
Y or N n you entered n Please enter your name: Test You entered Test is this what you want?
Y or N y you entered y Please enter your name:
Aucun commentaire:
Enregistrer un commentaire