samedi 22 avril 2017

Computer doesn't use variables values in the if statement [duplicate]

This question already has an answer here:

I am new to the Java programming language (I come from JavaScript and C++) and I wrote the below code. The outcome of my code will always be the code in the else block. Even if the if statement is valid. I don't understand because when I compile the code it doesn't return any errors. Here is my code: `import java.util.Scanner;

class myFirstJavaProgram{
    public static void main(String args[]){
        Scanner Bob = new Scanner(System.in);
        String password, p2;
    System.out.println("Please enter your desired password:");
password = Bob.next();
System.out.println("Please re enter your password:");
p2 = Bob.next();
        System.out.println("password is: " + password);
        System.out.println("p2 is: " + p2);
if(p2 == password){
System.out.println("It worked!");
}else{
System.out.println("It didn't work.");
}
}

}` Could someone please show me what I am doing wrong, I'm lost.

Aucun commentaire:

Enregistrer un commentaire