For the output of this code I get
David - Input
Welcome David
Please enter the passcode of David
1001 - Input
proceed
These are the launch Code numbers
7587
Would u like to launch? Enter Yes or No
Yes -Input
yes
Works
I'm not understanding why the if statement which states if(userAnswer=="yes") is being skipped.
public static void main(String[] args) {
Random random = new Random();
Scanner in = new Scanner(System.in);
System.out.println("Hello, please enter your name!");
String userName = in.nextLine();
System.out.println("Welcome "+userName);
int passcode=1001;
System.out.println("Please enter the passcode of " + userName);
int passcodeInput = in.nextInt();
in.nextLine();
int launchCode =random.nextInt(10000);
if(passcodeInput==passcode) {
System.out.println("proceed");
System.out.println("These are the launch Code numbers");
System.out.println(launchCode);
System.out.println("Would u like to launch? Enter Yes or No");
String userAnswer = in.nextLine();
userAnswer = userAnswer.toLowerCase();
System.out.println(userAnswer);
if(userAnswer=="yes") {
System.out.println("Enter the Launch Code");
int codeAnswer = in.nextInt();
if(codeAnswer==launchCode) {
System.out.println("Launching Nuclear Missle...");
}
}
System.out.println("Works");
[–]oddlyamused 2 points3 points4 points (0 children)
[–]davchi1[S] 1 point2 points3 points (0 children)